Skip to content

Commit 4713d84

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0143
1 parent da550ba commit 4713d84

File tree

6 files changed

+205
-49
lines changed

6 files changed

+205
-49
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
1. [合并 K 个排序链表](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README.md)
6363
1. [排序链表](/solution/0100-0199/0148.Sort%20List/README.md)
6464
1. [反转链表](/solution/0200-0299/0206.Reverse%20Linked%20List/README.md)
65+
1. [重排链表](/solution/0100-0199/0143.Reorder%20List/README.md)
6566
1. [回文链表](/solution/0200-0299/0234.Palindrome%20Linked%20List/README.md)
6667
1. [环形链表](/solution/0100-0199/0141.Linked%20List%20Cycle/README.md)
6768
1. [环形链表 II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README.md)

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
6060
1. [Merge k Sorted Lists](/solution/0000-0099/0023.Merge%20k%20Sorted%20Lists/README_EN.md)
6161
1. [Sort List](/solution/0100-0199/0148.Sort%20List/README_EN.md)
6262
1. [Reverse Linked List](/solution/0200-0299/0206.Reverse%20Linked%20List/README_EN.md)
63+
1. [Reorder List](/solution/0100-0199/0143.Reorder%20List/README_EN.md)
6364
1. [Palindrome Linked List](/solution/0200-0299/0234.Palindrome%20Linked%20List/README_EN.md)
6465
1. [Linked List Cycle](/solution/0100-0199/0141.Linked%20List%20Cycle/README_EN.md)
6566
1. [Linked List Cycle II](/solution/0100-0199/0142.Linked%20List%20Cycle%20II/README_EN.md)

solution/0100-0199/0143.Reorder List/README.md

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,98 @@
2222

2323
<!-- 这里可写通用的实现逻辑 -->
2424

25+
先通过快慢指针找到链表中点,将链表划分为左右两部分。之后反转右半部分的链表,然后将左右两个链接依次连接即可。
26+
2527
<!-- tabs:start -->
2628

2729
### **Python3**
2830

2931
<!-- 这里可写当前语言的特殊实现逻辑 -->
3032

3133
```python
32-
34+
# Definition for singly-linked list.
35+
# class ListNode:
36+
# def __init__(self, val=0, next=None):
37+
# self.val = val
38+
# self.next = next
39+
class Solution:
40+
def reorderList(self, head: ListNode) -> None:
41+
"""
42+
Do not return anything, modify head in-place instead.
43+
"""
44+
if head is None or head.next is None:
45+
return
46+
slow, fast = head, head.next
47+
# 快慢指针找到链表中点
48+
while fast and fast.next:
49+
slow, fast = slow.next, fast.next.next
50+
cur = slow.next
51+
slow.next = None
52+
pre = None
53+
# cur 指向右半部分的链表,反转
54+
while cur:
55+
t = cur.next
56+
cur.next = pre
57+
pre = cur
58+
cur = t
59+
cur = head
60+
61+
# 将左右链表依次连接
62+
while pre:
63+
t1 = cur.next
64+
cur.next = pre
65+
cur = t1
66+
t2 = pre.next
67+
pre.next = t1
68+
pre = t2
3369
```
3470

3571
### **Java**
3672

3773
<!-- 这里可写当前语言的特殊实现逻辑 -->
3874

3975
```java
40-
76+
/**
77+
* Definition for singly-linked list.
78+
* public class ListNode {
79+
* int val;
80+
* ListNode next;
81+
* ListNode() {}
82+
* ListNode(int val) { this.val = val; }
83+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
84+
* }
85+
*/
86+
class Solution {
87+
public void reorderList(ListNode head) {
88+
if (head == null || head.next == null) {
89+
return;
90+
}
91+
ListNode slow = head;
92+
ListNode fast = head.next;
93+
while (fast != null && fast.next != null) {
94+
slow = slow.next;
95+
fast = fast.next.next;
96+
}
97+
ListNode cur = slow.next;
98+
slow.next = null;
99+
ListNode pre = null;
100+
while (cur != null) {
101+
ListNode t = cur.next;
102+
cur.next = pre;
103+
pre = cur;
104+
cur = t;
105+
}
106+
cur = head;
107+
while (pre != null) {
108+
ListNode t1 = cur.next;
109+
cur.next = pre;
110+
cur = t1;
111+
ListNode t2 = pre.next;
112+
pre.next = cur;
113+
pre = t2;
114+
}
115+
}
116+
}
41117
```
42118

43119
### **...**

solution/0100-0199/0143.Reorder List/README_EN.md

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,83 @@ Given 1-&gt;2-&gt;3-&gt;4-&gt;5, reorder it to 1-&gt;5-&gt;2-&gt;4-&gt;3.
3131
### **Python3**
3232

3333
```python
34-
34+
# Definition for singly-linked list.
35+
# class ListNode:
36+
# def __init__(self, val=0, next=None):
37+
# self.val = val
38+
# self.next = next
39+
class Solution:
40+
def reorderList(self, head: ListNode) -> None:
41+
"""
42+
Do not return anything, modify head in-place instead.
43+
"""
44+
if head is None or head.next is None:
45+
return
46+
slow, fast = head, head.next
47+
while fast and fast.next:
48+
slow, fast = slow.next, fast.next.next
49+
cur = slow.next
50+
slow.next = None
51+
pre = None
52+
while cur:
53+
t = cur.next
54+
cur.next = pre
55+
pre = cur
56+
cur = t
57+
cur = head
58+
while pre:
59+
t1 = cur.next
60+
cur.next = pre
61+
cur = t1
62+
t2 = pre.next
63+
pre.next = t1
64+
pre = t2
3565
```
3666

3767
### **Java**
3868

3969
```java
40-
70+
/**
71+
* Definition for singly-linked list.
72+
* public class ListNode {
73+
* int val;
74+
* ListNode next;
75+
* ListNode() {}
76+
* ListNode(int val) { this.val = val; }
77+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
78+
* }
79+
*/
80+
class Solution {
81+
public void reorderList(ListNode head) {
82+
if (head == null || head.next == null) {
83+
return;
84+
}
85+
ListNode slow = head;
86+
ListNode fast = head.next;
87+
while (fast != null && fast.next != null) {
88+
slow = slow.next;
89+
fast = fast.next.next;
90+
}
91+
ListNode cur = slow.next;
92+
slow.next = null;
93+
ListNode pre = null;
94+
while (cur != null) {
95+
ListNode t = cur.next;
96+
cur.next = pre;
97+
pre = cur;
98+
cur = t;
99+
}
100+
cur = head;
101+
while (pre != null) {
102+
ListNode t1 = cur.next;
103+
cur.next = pre;
104+
cur = t1;
105+
ListNode t2 = pre.next;
106+
pre.next = cur;
107+
pre = t2;
108+
}
109+
}
110+
}
41111
```
42112

43113
### **...**

solution/0100-0199/0143.Reorder List/Solution.java

Lines changed: 22 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,39 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
1012
public void reorderList(ListNode head) {
11-
if (head == null || head.next == null || head.next.next == null) {
13+
if (head == null || head.next == null) {
1214
return;
1315
}
14-
15-
ListNode dummy = new ListNode(-1);
16-
dummy.next = head;
1716
ListNode slow = head;
18-
ListNode fast = head;
19-
ListNode pre = dummy;
17+
ListNode fast = head.next;
2018
while (fast != null && fast.next != null) {
21-
fast = fast.next.next;
2219
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;
20+
fast = fast.next.next;
3721
}
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-
22+
ListNode cur = slow.next;
23+
slow.next = null;
24+
ListNode pre = null;
25+
while (cur != null) {
26+
ListNode t = cur.next;
27+
cur.next = pre;
28+
pre = cur;
29+
cur = t;
5430
}
55-
56-
if (p4 != null) {
57-
p1.next.next = p4;
31+
cur = head;
32+
while (pre != null) {
33+
ListNode t1 = cur.next;
34+
cur.next = pre;
35+
cur = t1;
36+
ListNode t2 = pre.next;
37+
pre.next = cur;
38+
pre = t2;
5839
}
59-
head = dummy.next;
60-
61-
62-
6340
}
6441
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def reorderList(self, head: ListNode) -> None:
8+
"""
9+
Do not return anything, modify head in-place instead.
10+
"""
11+
if head is None or head.next is None:
12+
return
13+
slow, fast = head, head.next
14+
while fast and fast.next:
15+
slow, fast = slow.next, fast.next.next
16+
cur = slow.next
17+
slow.next = None
18+
pre = None
19+
while cur:
20+
t = cur.next
21+
cur.next = pre
22+
pre = cur
23+
cur = t
24+
cur = head
25+
while pre:
26+
t1 = cur.next
27+
cur.next = pre
28+
cur = t1
29+
t2 = pre.next
30+
pre.next = t1
31+
pre = t2

0 commit comments

Comments
 (0)