Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. # class ListNode:
  2. # def __init__(self, x):
  3. # self.val = x
  4. # self.next = None
  5.  
  6.  
  7. class Solution:
  8. def oddEvenList(self, head):
  9. if not head:
  10. return None
  11. i = 0
  12. tmp = head
  13. first = True
  14. prev = None
  15. tmp_even_head = None
  16. tail = None
  17. while tmp:
  18. if i % 2 != 0:
  19. if first:
  20. second_list = ListNode(tmp.val)
  21. tmp_even_head = second_list
  22. second_list.next = None
  23. first = False
  24. else:
  25. second_list.next = ListNode(tmp.val)
  26. second_list = second_list.next
  27. second_list.next = None
  28. if tmp and tmp.next:
  29. prev.next = tmp.next
  30. else:
  31. tail = tmp
  32. prev = tmp
  33. tmp = tmp.next
  34. i += 1
  35. if tmp_even_head:
  36. tail.next = tmp_even_head
  37. return head
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement