当前位置:   article > 正文

【python】listnode链表_python中listnode

python中listnode

Leetcode707:

Design your implementation of the linked list. You can choose to use a singly or doubly linked list.
A node in a singly linked list should have two attributes: val and nextval is the value of the current node, and next is a pointer/reference to the next node.
If you want to use the doubly linked list, you will need one more attribute prev to indicate the previous node in the linked list. Assume all nodes in the linked list are 0-indexed.

Implement the MyLinkedList class:

  • MyLinkedList() Initializes the MyLinkedList object.
  • int get(int index) Get the value of the indexth node in the linked list. If the index is invalid, return -1.
  • void addAtHead(int val) 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.
  • void addAtTail(int val) Append a node of value val as the last element of the linked list.
  • void addAtIndex(int index, int val) Add a node of value val before the indexth node in the linked list. If index equals the length of the linked list, the node will be appended to the end of the linked list. If index is greater than the length, the node will not be inserted.
  • void deleteAtIndex(int index) Delete the indexth node in the linked list, if the index is valid.
  1. class ListNode:
  2. def __init__(self, x):
  3. self.val = x
  4. self.next = None
  5. class MyLinkedList:
  6. def __init__(self):
  7. self.size = 0
  8. self.head = ListNode(0) # sentinel node as pseudo-head
  9. def get(self, index: int) -> int:
  10. """
  11. Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  12. """
  13. # if index is invalid
  14. if index < 0 or index >= self.size:
  15. return -1
  16. curr = self.head
  17. # index steps needed
  18. # to move from sentinel node to wanted index
  19. for _ in range(index + 1):
  20. curr = curr.next
  21. return curr.val
  22. def addAtHead(self, val: int) -> None:
  23. """
  24. 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.
  25. """
  26. self.addAtIndex(0, val)
  27. def addAtTail(self, val: int) -> None:
  28. """
  29. Append a node of value val to the last element of the linked list.
  30. """
  31. self.addAtIndex(self.size, val)
  32. def addAtIndex(self, index: int, val: int) -> None:
  33. """
  34. 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.
  35. """
  36. # If index is greater than the length,
  37. # the node will not be inserted.
  38. if index > self.size:
  39. return
  40. # [so weird] If index is negative,
  41. # the node will be inserted at the head of the list.
  42. if index < 0:
  43. index = 0
  44. self.size += 1
  45. # find predecessor of the node to be added
  46. pred = self.head
  47. for _ in range(index):
  48. pred = pred.next
  49. # node to be added
  50. to_add = ListNode(val)
  51. # insertion itself
  52. to_add.next = pred.next
  53. pred.next = to_add
  54. def deleteAtIndex(self, index: int) -> None:
  55. """
  56. Delete the index-th node in the linked list, if the index is valid.
  57. """
  58. # if the index is invalid, do nothing
  59. if index < 0 or index >= self.size:
  60. return
  61. self.size -= 1
  62. # find predecessor of the node to be deleted
  63. pred = self.head
  64. for _ in range(index):
  65. pred = pred.next
  66. # delete pred.next
  67. pred.next = pred.next.next
'
运行
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/859333
推荐阅读
相关标签
  

闽ICP备14008679号