链表

203. 移除链表元素

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点

  1. /**
  2. * Definition for singly-linked list.
  3. * function ListNode(val, next) {
  4. * this.val = (val===undefined ? 0 : val)
  5. * this.next = (next===undefined ? null : next)
  6. * }
  7. */
  8. /**
  9. * @param {ListNode} head
  10. * @param {number} val
  11. * @return {ListNode}
  12. */
  13. var removeElements = function(head, val) {
  14. const ret = new ListNode(0, head);
  15. let cur = ret;
  16. while(cur.next) {
  17. if(cur.next.val === val) {
  18. cur.next = cur.next.next;
  19. continue;
  20. }
  21. cur = cur.next;
  22. }
  23. return ret.next;
  24. };

707. 设计链表

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

  1. class LinkNode {
  2. constructor(val, next) {
  3. this.val = val;
  4. this.next = next;
  5. }
  6. }
  7. /**
  8. * Initialize your data structure here.
  9. * 单链表 储存头尾节点 和 节点数量
  10. */
  11. var MyLinkedList = function() {
  12. this._size = 0;
  13. this._tail = null;
  14. this._head = null;
  15. };
  16. /**
  17. * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
  18. * @param {number} index
  19. * @return {number}
  20. */
  21. MyLinkedList.prototype.getNode = function(index) {
  22. if(index < 0 || index >= this._size) return null;
  23. // 创建虚拟头节点
  24. let cur = new LinkNode(0, this._head);
  25. // 0 -> head
  26. while(index-- >= 0) {
  27. cur = cur.next;
  28. }
  29. return cur;
  30. };
  31. MyLinkedList.prototype.get = function(index) {
  32. if(index < 0 || index >= this._size) return -1;
  33. // 获取当前节点
  34. return this.getNode(index).val;
  35. };
  36. /**
  37. * 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.
  38. * @param {number} val
  39. * @return {void}
  40. */
  41. MyLinkedList.prototype.addAtHead = function(val) {
  42. const node = new LinkNode(val, this._head);
  43. this._head = node;
  44. this._size++;
  45. if(!this._tail) {
  46. this._tail = node;
  47. }
  48. };
  49. /**
  50. * Append a node of value val to the last element of the linked list.
  51. * @param {number} val
  52. * @return {void}
  53. */
  54. MyLinkedList.prototype.addAtTail = function(val) {
  55. const node = new LinkNode(val, null);
  56. this._size++;
  57. if(this._tail) {
  58. this._tail.next = node;
  59. this._tail = node;
  60. return;
  61. }
  62. this._tail = node;
  63. this._head = node;
  64. };
  65. /**
  66. * 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.
  67. * @param {number} index
  68. * @param {number} val
  69. * @return {void}
  70. */
  71. MyLinkedList.prototype.addAtIndex = function(index, val) {
  72. if(index > this._size) return;
  73. if(index <= 0) {
  74. this.addAtHead(val);
  75. return;
  76. }
  77. if(index === this._size) {
  78. this.addAtTail(val);
  79. return;
  80. }
  81. // 获取目标节点的上一个的节点
  82. const node = this.getNode(index - 1);
  83. node.next = new LinkNode(val, node.next);
  84. this._size++;
  85. };
  86. /**
  87. * Delete the index-th node in the linked list, if the index is valid.
  88. * @param {number} index
  89. * @return {void}
  90. */
  91. MyLinkedList.prototype.deleteAtIndex = function(index) {
  92. if(index < 0 || index >= this._size) return;
  93. if(index === 0) {
  94. this._head = this._head.next;
  95. this._size--;
  96. return;
  97. }
  98. // 获取目标节点的上一个的节点
  99. const node = this.getNode(index - 1);
  100. node.next = node.next.next;
  101. // 处理尾节点
  102. if(index === this._size - 1) {
  103. this._tail = node;
  104. }
  105. this._size--;
  106. };
  107. // MyLinkedList.prototype.out = function() {
  108. // let cur = this._head;
  109. // const res = [];
  110. // while(cur) {
  111. // res.push(cur.val);
  112. // cur = cur.next;
  113. // }
  114. // };
  115. /**
  116. * Your MyLinkedList object will be instantiated and called as such:
  117. * var obj = new MyLinkedList()
  118. * var param_1 = obj.get(index)
  119. * obj.addAtHead(val)
  120. * obj.addAtTail(val)
  121. * obj.addAtIndex(index,val)
  122. * obj.deleteAtIndex(index)
  123. */