Skip to content

Commit ef2597d

Browse files
committed
这题有毒
1 parent 6d5c783 commit ef2597d

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ Success is like pregnancy, Everybody congratulates you but nobody knows how many
2626
|-----|-------| -------- | ---- | ------|------------|---|-----|
2727
|142|[Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/#/description)| [Python](./linkedlist/detectCycleii.py) | _O(n)_| _O(1)_ | Medium |CC189 |[Video](https://www.youtube.com/watch?v=iZVBVCpmugI&t=1s)|
2828
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/#/description)| [Python](./linkedlist/oddEvenList.py) | _O(n)_| _O(1)_ | Medium | ||
29+
|143|[Reorder List](https://leetcode.com/problems/reorder-list/#/description)| [Python](./linkedlist/reorderList.py) | _O(n)_| _O(1)_ | Medium | ||

linkedlist/reorderList.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# 143. Reorder List
2+
# Author: Yu Zhou
3+
4+
# Given a singly linked list L: L0→L1→…→Ln-1→Ln,
5+
# reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…
6+
#
7+
# You must do this in-place without altering the nodes' values.
8+
#
9+
# For example,
10+
# Given {1,2,3,4}, reorder it to {1,4,2,3}.
11+
12+
# 思路
13+
# 这题的思路是将Node分制
14+
# 设置俩Runner,取中间值,在对中间值后续的Node进行reverse,之后根据顺序合并
15+
# 切记在将一个链表切分的时候,一定要将左边的链表末端指向Null,否则就没切断。
16+
17+
# ****************
18+
# Final Solution *
19+
# ****************
20+
class Solution(object):
21+
def reorderList(self, head):
22+
if not head:
23+
return
24+
mid = self.findMid(head)
25+
last_node = self.reverse(mid)
26+
self.merge(head, last_node)
27+
28+
def findMid(self,head):
29+
slow = fast = head
30+
while fast and fast.next:
31+
slow = slow.next
32+
fast = fast.next.next
33+
mid = slow.next
34+
slow.next = None
35+
36+
return mid
37+
38+
def reverse(self, node):
39+
prev = None
40+
cur = node
41+
42+
while cur:
43+
next = cur.next
44+
cur.next = prev
45+
prev = cur
46+
cur = next
47+
return prev
48+
49+
# Merge应该设置Next的Guardnode,因为每次我们将Cur Node指向两一个链表的Head Node的时候
50+
# 我们就缺失原有链表的Next的指针。
51+
def merge(self, head, end):
52+
cur1, cur2 = head, end
53+
while cur2:
54+
temp1, temp2 = cur1.next, cur2.next
55+
cur1.next = cur2
56+
cur2.next = temp1
57+
cur1 = temp1
58+
cur2 = temp2
59+
return
60+
61+
# ***************************************
62+
# The following code is an fail attempt *
63+
# ***************************************
64+
class Solution(object):
65+
def reorderList(self, head):
66+
if not head:
67+
return
68+
mid = self.mid_finder(head)
69+
last_node = self.reverse(mid)
70+
self.merge(head, last_node)
71+
72+
def mid_finder(self,head):
73+
slow = fast = head
74+
while fast and fast.next:
75+
slow = slow.next
76+
fast = fast.next.next
77+
#这里犯错了,应该把左半边的链表指向Null,不然切不断
78+
# 缺了slow.next = None
79+
return slow
80+
81+
def reverse(self, node):
82+
prev = None
83+
cur = node
84+
while cur:
85+
next = cur.next
86+
cur.next = prev
87+
prev = cur
88+
cur = next
89+
return prev
90+
91+
def merge(self, head, last):
92+
93+
first = head
94+
while last:
95+
first.next = last
96+
first = first.next
97+
last.next = first
98+
last = last.next
99+
return

0 commit comments

Comments
 (0)