Skip to content

Commit cf48cc3

Browse files
committed
rewrite
1 parent 883a1de commit cf48cc3

File tree

2 files changed

+50
-52
lines changed

2 files changed

+50
-52
lines changed

RemoveDuplicatesfromSortedListII.java

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1-
2-
3-
/**
4-
* Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
5-
*
6-
* For example,
7-
* Given 1->2->3->3->4->4->5, return 1->2->5.
8-
* Given 1->1->1->2->3, return 2->3.
1+
/**
2+
* Given a sorted linked list, delete all nodes that have duplicate numbers,
3+
* leaving only distinct numbers from the original list.
4+
*
5+
* For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3,
6+
* return 2->3.
97
*/
108

119
public class RemoveDuplicatesfromSortedListII {
1210
public ListNode deleteDuplicates(ListNode head) {
13-
ListNode begin = head;
14-
ListNode cur = head;
15-
ListNode newHead = null;
16-
ListNode newTail = null;
17-
while (cur != null) {
18-
while (cur.next != null && begin.val == cur.next.val) {
19-
cur = cur.next;
20-
}
21-
if (begin != cur) {
22-
begin = cur.next;
23-
cur = cur.next;
24-
} else {
25-
ListNode tmp = begin;
26-
begin = begin.next;
27-
cur = cur.next;
28-
if (newHead == null) {
29-
newHead = tmp;
30-
newTail = tmp;
11+
if (head == null)
12+
return head;
13+
ListNode start = new ListNode(0);
14+
start.next = head;
15+
ListNode slow = start;
16+
ListNode fast = head;
17+
while (fast.next != null) {
18+
if (slow.next.val != fast.next.val) {
19+
if (slow.next.next == fast.next) {
20+
slow = slow.next;
3121
} else {
32-
newTail.next = tmp;
33-
newTail = newTail.next;
22+
slow.next = fast.next;
3423
}
35-
newTail.next = null;
3624
}
25+
fast = fast.next;
26+
}
27+
if (slow.next.next != fast.next) {
28+
slow.next = fast.next;
3729
}
38-
return newHead;
30+
return start.next;
3931
}
4032
}

ReverseLinkedListII.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
/**
42
* Reverse a linked list from position m to n. Do it in-place and in one-pass.
53
*
@@ -12,32 +10,40 @@
1210

1311
public class ReverseLinkedListII {
1412
public ListNode reverseBetween(ListNode head, int m, int n) {
15-
if (head == null)
16-
return head;
17-
int start = 1, end = 0;
18-
ListNode s = head;
19-
ListNode p = null;
20-
while (start++ < m && s != null) {
21-
p = s;
22-
s = s.next;
13+
ListNode start = new ListNode(0);
14+
start.next = head;
15+
ListNode low = start;
16+
ListNode high = start;
17+
ListNode p1 = null;
18+
ListNode p2 = null;
19+
for (int i = 0; i < m - 1; i++) {
20+
low = low.next;
2321
}
24-
ListNode cur = null;
25-
ListNode next = null;
22+
p1 = low;
23+
low = low.next;
24+
for (int i = 0; i < n; i++) {
25+
high = high.next;
26+
}
27+
p2 = high.next;
28+
high.next = null;
29+
p1.next = reverseList(low);
30+
while (p1.next != null) {
31+
p1 = p1.next;
32+
}
33+
p1.next = p2;
34+
return start.next;
35+
}
36+
37+
private ListNode reverseList(ListNode low) {
38+
ListNode cur = low;
2639
ListNode prev = null;
27-
end = m;
28-
cur = s;
29-
while (end++ <= n && cur != null) {
40+
ListNode next = null;
41+
while (cur != null) {
3042
next = cur.next;
3143
cur.next = prev;
3244
prev = cur;
3345
cur = next;
3446
}
35-
s.next = cur;
36-
if (p != null) {
37-
p.next = prev;
38-
return head;
39-
} else {
40-
return prev;
41-
}
47+
return prev;
4248
}
4349
}

0 commit comments

Comments
 (0)