Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9d6043d

Browse files
added daly challenge
1 parent a2e844d commit 9d6043d

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎143. Reorder List.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /