Advertisement
kosievdmerwe

Untitled

Dec 1st, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution:
  7.     def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]:
  8.         heads = [None, None]
  9.         tails = [None, None]
  10.        
  11.         cur = head
  12.         idx = 0
  13.         while cur:
  14.             i = idx % 2
  15.             idx += 1
  16.             if heads[i] is None:
  17.                 heads[i] = tails[i] = cur
  18.             else:
  19.                 tails[i].next = cur
  20.                 tails[i] = cur
  21.             cur = cur.next
  22.        
  23.         if heads[0]:
  24.             tails[0].next = heads[1]
  25.         if tails[1]:
  26.             tails[1].next = None
  27.         return heads[0]
  28.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement