LeetCode刷题 Day03
203. Remove Linked List Elements
Given the head of a linked list and an integer val, remove all the nodes of the linked list that has Node.val == val, and return the new head.
Example 1:
Input: head = [1,2,6,3,4,5,6], val = 6
Output: [1,2,3,4,5]
Example 2:
Input: head = [], val = 1
Output: []
Example 3:
Input: head = [7,7,7,7], val = 7
Output: []
创造dummyHead
- 如果遇到target value就用node.next = node.next.next删除节点 否则移动指针到下一个
代码:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var removeElements = function(head, val) {
let dummyNode = new ListNode(null);
dummyNode.next = head;
let curr = dummyNode;
while (curr && curr.next) {
if (curr.next.val === val) {
curr.next = curr.next.next;
} else {
curr = curr.next;
}
}
return dummyNode.next;
};
时间复杂度: O(n) 空间复杂度: O(1)
707. Design Linked List
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 next. val 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 theMyLinkedListobject.int get(int index)Get the value of theindexthnode in the linked list. If the index is invalid, return-1.void addAtHead(int val)Add a node of valuevalbefore 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 valuevalas the last element of the linked list.void addAtIndex(int index, int val)Add a node of valuevalbefore theindexthnode in the linked list. Ifindexequals the length of the linked list, the node will be appended to the end of the linked list. Ifindexis greater than the length, the node will not be inserted.void deleteAtIndex(int index)Delete theindexthnode in the linked list, if the index is valid.
Example 1:
Input
["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"]
[[], [1], [3], [1, 2], [1], [1], [1]]
Output
[null, null, null, null, 2, null, 3]
Explanation
MyLinkedList myLinkedList = new MyLinkedList();
myLinkedList.addAtHead(1);
myLinkedList.addAtTail(3);
myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
myLinkedList.get(1); // return 2
myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
myLinkedList.get(1); // return 3
注意事项
- 新建一个getNode方法返回index对应node,getNode可以重复使用
- 注意题目中要求的一些边界情况this.size, this.size - 1
- 注意当链表为空的时候, 执行插入节点操作,要处理头结点和尾结点
- 当只有一个节点的时候,执行删除节点操作,要处理头结点和尾结点
代码:
class LinkNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
var MyLinkedList = function() {
this.size = 0;
this.head = null;
this.tail = null;
};
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.getNode = function(index) {
if (index < 0 || index >= this.size) return null;
let node = new LinkNode(0, this.head);
while (index-- >= 0) {
node = node.next;
}
return node;
};
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this.size) return -1;
return this.getNode(index).val;
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
const node = new LinkNode(val, this.head);
this.head = node;
this.size++;
// if the linked list is empty, then tail should be assigned
if (!this.tail) {
this.tail = node;
}
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
const node = new LinkNode(val, null);
this.size++;
if (this.tail) {
this.tail.next = node;
this.tail = this.tail.next;
return;
}
// if the linked list is empty, both of them should be assigned
this.tail = node;
this.head = node;
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
if (index > this.size) return;
if (index <= 0) {
this.addAtHead(val);
return;
}
if (index === this.size) {
this.addAtTail(val);
return;
}
let node = this.getNode(index - 1);
node.next = new LinkNode(val, node.next);
this.size++;
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this.size) return;
if (index === 0) {
this.head = this.head.next;
if (index === this.size - 1) {
this.tail = this.head;
}
this.size--;
return 0;
}
const node = this.getNode(index - 1);
node.next = node.next.next;
if (index === this.size - 1) {
this.tail = node;
}
this.size--;
};
/**
* Your MyLinkedList object will be instantiated and called as such:
* var obj = new MyLinkedList()
* var param_1 = obj.get(index)
* obj.addAtHead(val)
* obj.addAtTail(val)
* obj.addAtIndex(index,val)
* obj.deleteAtIndex(index)
*/
206. Reverse Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
Example 1:
Input: head = [1,2,3,4,5]
Output: [5,4,3,2,1]
可以想象一个动图
- 保存好当前节点的下一个节点,为跑到下一个节点做准备
- 当前节点保存好下一个节点之后,主动指向前一个节点
- 前一个节点被连通后,直接占据了当前节点的位置
- 当前节点跑到已经准备好的下一个节点
- 最终前一个节点成了head,当前节点是null
代码:
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
var reverseList = function(head) {
let curr = head;
let prev = null;
while (curr) {
let next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
};
时间复杂度: O(n) 空间复杂度: O(1)