-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path30_November.py
36 lines (28 loc) · 1014 Bytes
/
30_November.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
class Solution:
def reorderList(self,head):
slow = head
fast = head.next
#finding the middle element
while fast and fast.next:
fast = fast.next.next
slow = slow.next
#slow will be middle element we break at middle ele to have two seperate linked lists
second = slow.next
slow.next = None
prev = None
# now we reverse the second linked list
while second:
temp = second.next
second.next = prev
prev,second = second,temp
#assigning starting point of first and second linked list
first,second = head,prev
# now we merge these two linked list in the given order
while second:
tmp1 = first.next
tmp2 = second.next
first.next = second
second.next = tmp1
first = tmp1
second = tmp2
return head