Skip to content

Commit 5ca5de6

Browse files
author
turingfly
committed
Linked List
1 parent 0709560 commit 5ca5de6

File tree

5 files changed

+222
-0
lines changed

5 files changed

+222
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package chapter02LinkedList;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 19, 2017 9:33:21 PM
7+
*
8+
* Problem: Implement an algorithm to delete a node in the middle (i.e.,
9+
* any node but the first and last node, not necessarily the exact
10+
* middle) of a singly linked list, given only access to that node.
11+
*
12+
* EXAMPLE lnput:the node c from the linked list a->b->c->d->e->f
13+
* Result: nothing is returned, but the new linked list looks like
14+
* a->b->d->e->f
15+
*
16+
* Solution:
17+
*
18+
*/
19+
public class DeleteMiddleNode {
20+
public boolean deleteNode(ListNode node) {
21+
if (node == null || node.next == null) {
22+
return false;
23+
}
24+
ListNode nextNode = node.next;
25+
node.val = nextNode.val;
26+
node.next = nextNode.next;
27+
return true;
28+
}
29+
}

src/chapter02LinkedList/ListNode.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package chapter02LinkedList;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
7+
public ListNode(int val) {
8+
this.val = val;
9+
}
10+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package chapter02LinkedList;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 19, 2017 9:38:52 PM
7+
*
8+
* Problem: Given a linked list and a value x, partition it such that all nodes
9+
* less than x come before nodes greater than or equal to x.
10+
* You should preserve the original relative order of the nodes in each of the
11+
* two partitions.
12+
* For example, Given 1->4->3->2->5->2 and x = 3, return 1->2->2->4->3->5.
13+
*
14+
*
15+
*/
16+
public class Partition {
17+
public ListNode partition(ListNode head, int x) {
18+
ListNode less = new ListNode(0);
19+
ListNode greater = new ListNode(0);
20+
ListNode l1 = less;
21+
ListNode l2 = greater;
22+
while (head != null) {
23+
if (head.val < x) {
24+
l1.next = head;
25+
l1 = head;
26+
} else {
27+
l2.next = head;
28+
l2 = head;
29+
}
30+
head = head.next;
31+
}
32+
// make sure there is no cycle.
33+
// 3->4->1->2. 4->2 change to 4->null
34+
l2.next = null;
35+
l1.next = greater.next;
36+
return less.next;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package chapter02LinkedList;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
/**
7+
*
8+
* @author chengfeili
9+
* Jun 19, 2017 7:40:58 PM
10+
*
11+
* Problem: Write code to remove duplicates from an unsorted linked
12+
* list. 1 -> 2 -> 1 ===> 1 -> 2
13+
* Solution:
14+
*
15+
*/
16+
public class RemoveDuplicates {
17+
// method 1: with temporary buffer.
18+
public ListNode deleteDups(ListNode node) {
19+
if (node == null) {
20+
return null;
21+
}
22+
ListNode head = node;
23+
Set<Integer> set = new HashSet<>();
24+
while (node.next != null) {
25+
if (set.contains(node.next.val)) {
26+
node.next = node.next.next;
27+
} else {
28+
set.add(node.val);
29+
node = node.next;
30+
}
31+
}
32+
return head;
33+
}
34+
35+
// method 2: without temporary buffer.
36+
// two pointers. time O(N2), space O(1)
37+
public ListNode deleteDups2(ListNode node) {
38+
ListNode head = node;
39+
while (node != null) {
40+
ListNode runner = node;
41+
while (runner.next != null) {
42+
if (runner.next.val == node.val) {
43+
runner.next = runner.next.next;
44+
} else {
45+
runner = runner.next;
46+
}
47+
}
48+
node = node.next;
49+
}
50+
return head;
51+
}
52+
53+
public static void main(String[] args) {
54+
ListNode n1 = new ListNode(1);
55+
ListNode n2 = new ListNode(2);
56+
ListNode n3 = new ListNode(1);
57+
n1.next = n2;
58+
n2.next = n3;
59+
RemoveDuplicates rmd = new RemoveDuplicates();
60+
ListNode node = rmd.deleteDups(n1);
61+
while (node != null) {
62+
System.out.print(node.val + " ");
63+
node = node.next;
64+
}
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package chapter02LinkedList;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 19, 2017 8:38:57 PM
7+
*
8+
* Problem: Implement an algorithm to find the kth to last element of a
9+
* singly linked list.
10+
* 1 -> 2 -> 3 3th to last node is 1
11+
*
12+
* Solution:
13+
*
14+
*/
15+
class Index {
16+
public int value = 0;
17+
}
18+
19+
public class ReturnKthToLast {
20+
21+
// method 1: recursion; Space O(N)
22+
public int printKthToLast(ListNode head, int k) {
23+
if (head == null) {
24+
return 0;
25+
}
26+
int index = printKthToLast(head.next, k) + 1;
27+
if (index == k) {
28+
System.out.println(k + "th to last node is " + head.val);
29+
}
30+
return index;
31+
}
32+
33+
// method 2: Wrapper class, recursion; Space O(N)
34+
public ListNode kthToLast(ListNode head, int k) {
35+
Index idx = new Index();
36+
return helper(head, k, idx);
37+
}
38+
39+
private ListNode helper(ListNode head, int k, Index idx) {
40+
if (head == null) {
41+
return null;
42+
}
43+
ListNode node = helper(head.next, k, idx);
44+
idx.value = idx.value + 1;
45+
if (idx.value == k) {
46+
return head;
47+
}
48+
return node;
49+
}
50+
51+
// mehtod 3: iteration Time: O(N), Space O(1)
52+
public ListNode kthToLast3(ListNode node, int k) {
53+
ListNode left = node;
54+
ListNode right = node;
55+
for (int i = 0; i < k; i++) {
56+
if (right == null) {
57+
return null;
58+
}
59+
System.out.println("==" + right.val);
60+
right = right.next;
61+
}
62+
while (right != null) {
63+
right = right.next;
64+
left = left.next;
65+
}
66+
return left;
67+
}
68+
69+
public static void main(String[] args) {
70+
ListNode n1 = new ListNode(1);
71+
ListNode n2 = new ListNode(2);
72+
ListNode n3 = new ListNode(3);
73+
n1.next = n2;
74+
n2.next = n3;
75+
ReturnKthToLast rmd = new ReturnKthToLast();
76+
// rmd.printKthToLast(n1, 3);
77+
System.out.println(rmd.kthToLast3(n1, 1).val);
78+
}
79+
}

0 commit comments

Comments
 (0)