Skip to content

Commit 1450d6b

Browse files
Reorder List
1 parent 6bea5ca commit 1450d6b

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ Your ideas/fixes/algorithms are more than welcome!
161161
|149|[Max Points on a Line](https://leetcode.com/problems/max-points-on-a-line/)|[Solution](../../blob/master/src/stevesun/algorithms/MaxPointsonaLine.java)| O(?)|O(?) | Hard|
162162
|145|[Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/)|[Solution](../../blob/master/src/stevesun/algorithms/BinaryTreePostOrderTraversal.java)| O(n)|O(h) | Hard| Binary Tree
163163
|144|[Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/)|[Solution](../../blob/master/src/stevesun/algorithms/BinaryTreePreorderTraversal.java)| O(n)|O(h) | Medium| Binary Tree
164+
|143|[Reorder List](https://leetcode.com/problems/reorder-list/)|[Solution](../../blob/master/src/stevesun/algorithms/ReorderList.java)| O(n)|O(1) | Medium|
164165
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/)|[Solution](../../blob/master/src/stevesun/algorithms/LinkedListCycleII.java)| O(n)|O(1) | Medium| Linked List
165166
|141|[Linked List Cycle](https://leetcode.com/problems/linked-list-cycle/)|[Solution](../../blob/master/src/stevesun/algorithms/LinkedListCycle.java)| O(n)|O(1) | Easy| Linked List
166167
|140|[Word Break II](https://leetcode.com/problems/word-break-ii/)|[Solution](../../blob/master/src/stevesun/algorithms/WordBreakII.java)| ? |O(n^2) | Hard| Backtracking/DFS
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package stevesun.algorithms;
2+
3+
import stevesun.common.classes.ListNode;
4+
5+
/**
6+
* Given a singly linked list L: L0→L1→…→Ln-1→Ln,
7+
reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
8+
9+
You must do this in-place without altering the nodes' values.
10+
11+
For example,
12+
Given {1,2,3,4}, reorder it to {1,4,2,3}.
13+
*/
14+
public class ReorderList {
15+
16+
public void reorderList(ListNode head) {
17+
if (head == null || head.next == null) {
18+
return;
19+
}
20+
/* first we use two pointers to separate this list into two parts */
21+
ListNode slowNode = head, fastNode = head;
22+
while (fastNode.next != null) {
23+
fastNode = fastNode.next;
24+
if (fastNode.next != null) {
25+
fastNode = fastNode.next;
26+
} else {
27+
break;
28+
}
29+
slowNode = slowNode.next;
30+
}
31+
// two sublist heads
32+
ListNode head1 = head, head2 = slowNode.next;
33+
// detach the two sublists;
34+
slowNode.next = null;
35+
36+
// reverse the second sublist
37+
ListNode cur = head2, post = cur.next;
38+
cur.next = null;
39+
while (post != null) {
40+
ListNode temp = post.next;
41+
post.next = cur;
42+
cur = post;
43+
post = temp;
44+
}
45+
head2 = cur;// the new head of the reversed sublist
46+
47+
// merge the two sublists as required
48+
ListNode p = head1, q = head2;
49+
while (q != null) {
50+
ListNode temp1 = p.next;
51+
ListNode temp2 = q.next;
52+
p.next = q;
53+
q.next = temp1;
54+
p = temp1;
55+
q = temp2;
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)