We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a2e844d commit 9d6043dCopy full SHA for 9d6043d
143. Reorder List.py
@@ -0,0 +1,39 @@
1
+# Definition for singly-linked list.
2
+# class ListNode(object):
3
+# def __init__(self, val=0, next=None):
4
+# self.val = val
5
+# self.next = next
6
+class Solution(object):
7
+ # Function for reversing
8
+ def reverse(self, head):
9
+ prev = None
10
+ current = head
11
+ while current:
12
+ prev, prev.next, current = current, prev, current.next
13
+ return prev
14
+
15
+ def reorderList(self, head):
16
+ """
17
+ :type head: ListNode
18
+ :rtype: None Do not return anything, modify head in-place instead.
19
20
+ if head is None:
21
+ return
22
23
+ fast = head.next
24
+ slow = head
25
+ # Catch the Middle of List (slow)
26
+ while fast and fast.next:
27
+ fast = fast.next.next
28
+ slow = slow.next
29
+ # Cut Middle of list then Reverse it
30
+ rev = self.reverse(slow.next)
31
+ slow.next = None
32
33
+ while rev:
34
+ h_next = head.next
35
+ r_next = rev.next
36
+ head.next = rev
37
+ rev.next = h_next
38
+ rev = r_next
39
+ head = h_next
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments