File tree 3 files changed +153
-0
lines changed
solution/143.Reorder List
3 files changed +153
-0
lines changed Original file line number Diff line number Diff line change @@ -69,6 +69,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
69
69
| 130 | [ Surrounded Regions] ( https://github.com/doocs/leetcode/tree/master/solution/130.Surrounded%20Regions ) | ` Depth-first Search ` , ` Breadth-first Search ` , ` Union Find ` |
70
70
| 137 | [ Single Number II] ( https://github.com/doocs/leetcode/tree/master/solution/137.Single%20Number%20II ) | ` Bit Manipulation ` |
71
71
| 142 | [ Linked List Cycle II] ( https://github.com/doocs/leetcode/tree/master/solution/142.Linked%20List%20Cycle%20II ) | ` Linked List ` , ` Two Pointers ` |
72
+ | 143| [ Reorder List] ( https://github.com/doocs/leetcode/tree/master/solution/143.Reorder%20List ) | ` Linked List ` |
72
73
| 144 | [ Binary Tree Preorder Traversal] ( https://github.com/doocs/leetcode/tree/master/solution/144.Binary%20Tree%20Preorder%20Traversal ) | ` Stack ` , ` Tree ` |
73
74
| 150 | [ Evaluate Reverse Polish Notation] ( https://github.com/doocs/leetcode/tree/master/solution/150.Evaluate%20Reverse%20Polish%20Notation ) | ` Stack ` |
74
75
| 153 | [ Find Minimum in Rotated Sorted Array] ( https://github.com/doocs/leetcode/tree/master/solution/153.Find%20Minimum%20in%20Rotated%20Sorted%20Array ) | ` Array ` , ` Binary Search ` |
Original file line number Diff line number Diff line change
1
+ ## 重排链表
2
+ ### 题目描述
3
+
4
+ 给定一个单链表 L:L0→L1→…→Ln-1→Ln ,
5
+ 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→…
6
+
7
+ 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
8
+
9
+ ** 示例 1:**
10
+ ```
11
+ 给定链表 1->2->3->4, 重新排列为 1->4->2->3.
12
+ ```
13
+
14
+ ** 示例 2:**
15
+ ```
16
+ 给定链表 1->2->3->4->5, 重新排列为 1->5->2->4->3.
17
+ ```
18
+
19
+ ### 解法
20
+ 先利用快慢指针找到链表的中间节点,之后对右半部分的链表进行` reverse ` 。最后对两个链表进行合并。注意边界的判断。
21
+
22
+
23
+ ``` java
24
+ /**
25
+ * Definition for singly-linked list.
26
+ * public class ListNode {
27
+ * int val;
28
+ * ListNode next;
29
+ * ListNode(int x) { val = x; }
30
+ * }
31
+ */
32
+ class Solution {
33
+ public void reorderList (ListNode head ) {
34
+ if (head == null || head. next == null || head. next. next == null ) {
35
+ return ;
36
+ }
37
+
38
+ ListNode dummy = new ListNode (- 1 );
39
+ dummy. next = head;
40
+ ListNode slow = head;
41
+ ListNode fast = head;
42
+ ListNode pre = dummy;
43
+ while (fast != null && fast. next != null ) {
44
+ fast = fast. next. next;
45
+ slow = slow. next;
46
+ pre = pre. next;
47
+ }
48
+
49
+ pre. next = null ;
50
+
51
+ // 将后半段的链表进行 reverse
52
+ ListNode rightPre = new ListNode (- 1 );
53
+ rightPre. next = null ;
54
+ ListNode t = null ;
55
+ while (slow != null ) {
56
+ t = slow. next;
57
+ slow. next = rightPre. next;
58
+ rightPre. next = slow;
59
+ slow = t;
60
+ }
61
+
62
+ ListNode p1 = dummy. next;
63
+ ListNode p2 = p1. next;
64
+ ListNode p3 = rightPre. next;
65
+ ListNode p4 = p3. next;
66
+ while (p1 != null ) {
67
+ p3. next = p1. next;
68
+ p1. next = p3;
69
+ if (p2 == null ) {
70
+ break ;
71
+ }
72
+ p1 = p2;
73
+ p2 = p1. next;
74
+ p3 = p4;
75
+ p4 = p3. next;
76
+
77
+ }
78
+
79
+ if (p4 != null ) {
80
+ p1. next. next = p4;
81
+ }
82
+ head = dummy. next;
83
+
84
+
85
+
86
+ }
87
+ }
88
+ ```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * public class ListNode {
4
+ * int val;
5
+ * ListNode next;
6
+ * ListNode(int x) { val = x; }
7
+ * }
8
+ */
9
+ class Solution {
10
+ public void reorderList (ListNode head ) {
11
+ if (head == null || head .next == null || head .next .next == null ) {
12
+ return ;
13
+ }
14
+
15
+ ListNode dummy = new ListNode (-1 );
16
+ dummy .next = head ;
17
+ ListNode slow = head ;
18
+ ListNode fast = head ;
19
+ ListNode pre = dummy ;
20
+ while (fast != null && fast .next != null ) {
21
+ fast = fast .next .next ;
22
+ slow = slow .next ;
23
+ pre = pre .next ;
24
+ }
25
+
26
+ pre .next = null ;
27
+
28
+ // 将后半段的链表进行 reverse
29
+ ListNode rightPre = new ListNode (-1 );
30
+ rightPre .next = null ;
31
+ ListNode t = null ;
32
+ while (slow != null ) {
33
+ t = slow .next ;
34
+ slow .next = rightPre .next ;
35
+ rightPre .next = slow ;
36
+ slow = t ;
37
+ }
38
+
39
+ ListNode p1 = dummy .next ;
40
+ ListNode p2 = p1 .next ;
41
+ ListNode p3 = rightPre .next ;
42
+ ListNode p4 = p3 .next ;
43
+ while (p1 != null ) {
44
+ p3 .next = p1 .next ;
45
+ p1 .next = p3 ;
46
+ if (p2 == null ) {
47
+ break ;
48
+ }
49
+ p1 = p2 ;
50
+ p2 = p1 .next ;
51
+ p3 = p4 ;
52
+ p4 = p3 .next ;
53
+
54
+ }
55
+
56
+ if (p4 != null ) {
57
+ p1 .next .next = p4 ;
58
+ }
59
+ head = dummy .next ;
60
+
61
+
62
+
63
+ }
64
+ }
You can’t perform that action at this time.
0 commit comments