-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReorder List.py
62 lines (49 loc) · 1.45 KB
/
Reorder List.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
"""
Do not return anything, modify head in-place instead.
"""
ptr = head
count_elements = 0
while ptr:
count_elements += 1
ptr = ptr.next
ptr = head
head2 = None
count_nodes = 0
while ptr:
if count_elements % 2 == 0 and count_nodes == (count_elements // 2)-1:
head2 = ptr.next
ptr.next = None
break
elif count_elements % 2 == 1 and count_nodes == (count_elements // 2):
head2 = ptr.next
ptr.next = None
break
ptr = ptr.next
count_nodes +=1
head2 = self.reverseLL(head2)
ptr1 , ptr2 = head , head2
temp1 , temp2 = None, None
while ptr1 and ptr2:
temp1 = ptr1.next
temp2 = ptr2.next
ptr1.next = ptr2
ptr2.next = temp1
ptr1 = temp1
ptr2 = temp2
def reverseLL(self,head):
ptr = new = None
current = head
while current:
ptr = current
current = current.next
ptr.next = new
new = ptr
head = new
return head