代码随想录算法训练营Day3 | 203.移除链表元素,707.设计链表,206.反转链表
203.移除链表元素
思路
重点是要清楚怎么用js写链表。
考虑三种情况,删除头节点,删除中间节点,删除尾节点
开头用空节点指向头节点,开始往后遍历+对比,如果碰到了要删除的节点,可以令last.next=cur.next;cur.next=null;最后返回head
代码
/**
* 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) {
const ret = new ListNode(0, head);
let cur = ret;
while(cur.next) {
if(cur.next.val == val) {
cur.next = cur.next.next;
continue;
}
cur = cur.next;
}
return ret.next;
};
707.设计链表
思路
get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
思路:遍历链表
addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
思路:插入头节点。
addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
思路:遍历链表到最后一个节点,插入尾节点。
addAtIndex(index,val):在链表中第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
思路:遍历链表。
代码
class LinkNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
var MyLinkedList = function() {
this._size=0;
this._head=null;
};
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
if(index >= this._size || index<0) return -1;
return this.getNode(index).val;
};
MyLinkedList.prototype.getNode = function(index) {
let cur = new LinkNode(0, this._head);
while(index >= 0) {
cur = cur.next;
index--;
}
return cur;
}
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
let newHead = new LinkNode(val, this._head);
this._head = newHead;
this._size++;
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
let p = new LinkNode(val, null), index = this._size-1;
let cur = new LinkNode(0, this._head);
// 如果链表为空,相当于插入头节点
if(this._size==0) this._head=p;
this.getNode(index).next = p;
this._size++;
};
/**
* @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 p = new LinkNode(val, null);
let cur = this.getNode(index-1);
p.next = cur.next;
cur.next = p;
this._size++;
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
if(index < 0 || index >= this._size) return;
if(index === 0 && this._size === 1) this._head=null;
else if(index === 0 && this._size > 1) {
this._head = this._head.next;
}
else {
let cur = this.getNode(index-1);
cur.next = cur.next.next;
}
this._size--;
};
206.反转链表
思路
遍历链表,让后一个节点指向前一个节点。
代码
/**
* 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) {
if(!head || !head.next) return head;
let temp = null, pre = null, cur = head;
while(cur) {
temp = cur.next;
cur.next = pre;
pre = cur;
cur = temp;
}
return pre;
};