-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07-LinkedListOddEven.py
More file actions
19 lines (19 loc) · 896 Bytes
/
07-LinkedListOddEven.py
File metadata and controls
19 lines (19 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
"""
Group all odd indexed nodes together, and even indexed nodes together
Head is 1, so odd
"""
if not head or not head.next: # Safety check
return head
odd = head
evenTail = head.next
while evenTail: # Till we reach end of list
if evenTail.next:
evenHead = odd.next # A node for where Even Group starts
odd.next = evenTail.next # Inserting the next odd between current even and odd
evenTail.next = evenTail.next.next # Grouping even, since odd was recently shifted
odd.next.next = evenHead # Pointing end of Odd to start of Even
# Move forward
odd = odd.next
evenTail = evenTail.next
return head