Skip to content

Commit ff63fdb

Browse files
committed
linked list code
1 parent 0da6022 commit ff63fdb

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.cszjo.lintcode.link;
2+
3+
import java.util.List;
4+
5+
/**
6+
* Given a linked list and a value x, partition it such that all nodes
7+
less than x come before nodes greater than or equal to x.
8+
9+
You should preserve the original relative order of the nodes
10+
in each of the two partitions.
11+
12+
For example,
13+
Given 1->4->3->2->5->2->null and x = 3,
14+
return 1->2->2->4->3->5->null.
15+
https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/73005
16+
* Created by hansiming on 2018/2/2.
17+
*/
18+
public class PartitionList {
19+
20+
public ListNode partition(ListNode head, int x) {
21+
// write your code here
22+
if (head == null || head.next == null)
23+
return head;
24+
ListNode left = new ListNode(0);
25+
ListNode right = new ListNode(0);
26+
ListNode leftIndex = left;
27+
ListNode rightIndex = right;
28+
ListNode node = head;
29+
while (node != null) {
30+
if (node.val < x) {
31+
leftIndex.next = node;
32+
leftIndex = leftIndex.next;
33+
} else {
34+
rightIndex.next = node;
35+
rightIndex = rightIndex.next;
36+
}
37+
node = node.next;
38+
}
39+
// right之后可能还有其他节点
40+
rightIndex.next = null;
41+
leftIndex.next = right.next;
42+
return left.next;
43+
}
44+
45+
public static void main(String[] args) {
46+
ListNode left = new ListNode(1);
47+
ListNode right = new ListNode(1);
48+
left.next = right;
49+
new PartitionList().partition(left, 0);
50+
}
51+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.cszjo.lintcode.link;
2+
3+
/**
4+
* Remove Duplicates from Sorted List
5+
* https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/73002
6+
* Created by hansiming on 2018/2/2.
7+
*/
8+
public class RemoveDuplicateNode {
9+
10+
// 保持当前节点值不变,直到遇到不同的值,修改节点指针
11+
public ListNode deleteDuplicates(ListNode head) {
12+
if (head == null || head.next == null)
13+
return head;
14+
ListNode node = head;
15+
while (node.next != null) {
16+
if (node.val == node.next.val) {
17+
node.next = node.next.next;
18+
} else {
19+
node = node.next;
20+
}
21+
}
22+
return head;
23+
}
24+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.cszjo.lintcode.link;
2+
3+
/**
4+
* Given a sorted linked list, delete all nodes that have duplicate numbers,
5+
leaving only distinct numbers from the original list.
6+
7+
Example
8+
Given 1->2->3->3->4->4->5, return 1->2->5.
9+
Given 1->1->1->2->3, return 2->3.
10+
* https://www.kancloud.cn/kancloud/data-structure-and-algorithm-notes/73003
11+
* Created by hansiming on 2018/2/2.
12+
*/
13+
public class RemoveDuplicateNode2 {
14+
15+
// 保持当前节点值不变,直到遇到不同的值,修改节点指针
16+
public ListNode deleteDuplicates(ListNode head) {
17+
// write your code here
18+
if (head == null || head.next == null)
19+
return head;
20+
ListNode begin = new ListNode(0);
21+
begin.next = head;
22+
ListNode node = begin;
23+
while (node.next != null) {
24+
int val = node.next.val;
25+
if (node.next.next != null && node.next.next.val == val) {
26+
while (node.next.next != null && node.next.next.val == val) {
27+
node.next = node.next.next;
28+
}
29+
node.next = node.next.next;
30+
} else {
31+
node = node.next;
32+
}
33+
}
34+
return begin.next;
35+
}
36+
37+
public static void main(String[] args) {
38+
ListNode listNode1 = new ListNode(3);
39+
ListNode listNode2 = new ListNode(3);
40+
ListNode listNode3 = new ListNode(2);
41+
ListNode listNode4 = new ListNode(3);
42+
ListNode listNode5 = new ListNode(3);
43+
ListNode listNode6 = new ListNode(3);
44+
ListNode listNode7 = new ListNode(4);
45+
listNode1.next = listNode2;
46+
listNode2.next = listNode3;
47+
listNode3.next = listNode4;
48+
listNode4.next = listNode5;
49+
listNode5.next = listNode6;
50+
listNode6.next = listNode7;
51+
new RemoveDuplicateNode2().deleteDuplicates(listNode1);
52+
}
53+
}

0 commit comments

Comments
 (0)