Guest User

Untitled

a guest
Jul 11th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. class Node:
  2. def __init__(self, val):
  3. self.val = val
  4. self.next = None
  5.  
  6.  
  7. class MyLinkedList:
  8. def __init__(self):
  9. """
  10. Initialize your data structure here.
  11. """
  12. self.head = None
  13.  
  14. def get(self, index):
  15. """
  16. Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  17. :type index: int
  18. :rtype: int
  19. """
  20. cur = self.head
  21.  
  22. while index:
  23. if index == 0 and cur:
  24. return cur.val
  25. index -= 1
  26.  
  27. if cur: # if cur then go cur.next
  28. cur = cur.next
  29. else: # cur is None then the index is invalid
  30. return -1
  31.  
  32. def addAtHead(self, val):
  33. """
  34. Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
  35. :type val: int
  36. :rtype: void
  37. """
  38. new_head = Node(val)
  39. new_head.next = self.head
  40. self.head = new_head
  41.  
  42. def addAtTail(self, val):
  43. """
  44. Append a node of value val to the last element of the linked list.
  45. :type val: int
  46. :rtype: void
  47. """
  48. cur = self.head
  49. if cur: # non-empty linked list
  50. while cur.next:
  51. cur = cur.next
  52. cur.next = Node(val)
  53. else: # empty linked list
  54. self.head = Node(val)
  55.  
  56. def addAtIndex(self, index, val):
  57. """
  58. Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
  59. :type index: int
  60. :type val: int
  61. :rtype: void
  62. """
  63.  
  64. if index == 0:
  65. self.addAtHead(val)
  66. return
  67.  
  68. cur = self.head
  69. prev = None
  70. while index and cur:
  71. prev = cur
  72. cur = cur.next
  73. index -= 1
  74. if prev:
  75. if cur:
  76. new = Node(val)
  77. new.next = cur
  78. prev.next = new
  79. else:
  80. prev.next = Node(val)
  81.  
  82. def deleteAtIndex(self, index):
  83. """
  84. Delete the index-th node in the linked list, if the index is valid.
  85. :type index: int
  86. :rtype: void
  87. """
  88.  
  89. cur = self.head
  90. prev = None
  91. while index and cur:
  92. prev = cur
  93. cur = cur.next
  94. index -= 1
  95. if prev and cur:
  96. prev.next = cur.next
  97.  
  98. def __repr__(self):
  99. s = ''
  100. cur = self.head
  101. while cur:
  102. s += str(cur.val) + ' -> '
  103. cur = cur.next
  104. return 'linked_list:' + str(s if self.head else 'None')
  105.  
  106.  
  107. import time
  108.  
  109. # funcs = ["addAtHead","deleteAtIndex","addAtHead","addAtHead","addAtHead","addAtIndex","addAtHead","addAtHead","deleteAtIndex","deleteAtIndex","addAtHead","addAtTail","addAtHead","addAtIndex","addAtIndex","addAtHead","get","addAtHead","addAtIndex","addAtIndex","addAtHead","addAtTail","get","addAtHead","deleteAtIndex","get","get","addAtIndex","addAtTail","get","get","addAtTail","addAtHead","addAtTail","addAtTail","addAtHead","get","addAtIndex","get","addAtHead","addAtHead","addAtTail","addAtHead","get","addAtTail","addAtHead","addAtHead","get","addAtHead","addAtHead","addAtHead","addAtIndex","get","addAtTail","addAtHead","addAtHead","addAtTail","get","addAtIndex","deleteAtIndex","get","addAtTail","addAtTail","get","addAtIndex","addAtIndex","addAtTail","addAtHead","addAtHead","addAtTail","addAtTail","addAtTail","addAtHead","addAtIndex","addAtTail","addAtHead","addAtTail","addAtTail","addAtTail","deleteAtIndex","deleteAtIndex","addAtTail","addAtHead","addAtTail","addAtTail","get","addAtIndex","addAtTail","addAtHead","addAtTail","addAtIndex","addAtHead","addAtTail","addAtIndex","addAtIndex","addAtIndex","addAtTail","addAtTail","addAtHead","deleteAtIndex","addAtIndex"]
  110. # params = [[24],[1],[79],[10],[51],[3,37],[92],[53],[6],[5],[6],[33],[47],[7,42],[6,40],[82],[9],[37],[6,84],[2,50],[21],[22],[6],[51],[7],[10],[2],[3,5],[8],[15],[6],[89],[32],[99],[65],[51],[9],[15,62],[20],[83],[10],[3],[10],[3],[21],[22],[22],[7],[87],[73],[85],[19,17],[29],[2],[23],[88],[21],[6],[20,46],[4],[38],[59],[36],[36],[32,99],[23,79],[84],[82],[89],[98],[48],[75],[55],[4,24],[90],[8],[24],[7],[17],[37],[10],[32],[15],[9],[18],[19],[53,96],[31],[11],[96],[4,44],[69],[65],[33,22],[38,55],[31,31],[12],[0],[47],[65],[54,37]]
  111.  
  112. funcs = ["addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
  113. params = [[1], [3], [1, 2], [1], [1], [1]]
  114.  
  115. obj = MyLinkedList()
  116. for idx, item in enumerate(funcs):
  117. command = 'obj.{}(*'.format(item) + str(params[idx]) + ')'
  118. print(command)
  119. exec(command)
  120. print(obj)
  121. time.sleep(1)
Add Comment
Please, Sign In to add comment