前言
之前自己刷题的时候,根本就不知道链表是什么,怎么传参数,怎么运行。这次看了好久的解法和视频讲解,才理解链表是怎么一回事,涨知识了。
203. 移除链表元素
解题思路
理解了链表之后,感觉这题还是比较简单的,在纸上画一下,就大概能想出来
var removeElements = function(head, val) {
const dummyHead=new ListNode(0);//定义一个虚拟头节点
dummyHead.next=head;//虚拟头节点的下一个指向头节点
let temp= dummyHead;
while (temp.next !== null) {
if(temp.next.val===val){
temp.next=temp.next.next
}else{
temp=temp.next
}
}
return dummyHead.next
};
707.设计链表
解题思路卡哥
解题思路官网
这道题对我来说突破了智商上限,目前还不太能全部理解,尽力了,之后有时间在自习研究研究,先把解法放上吧。
单向链表
暴力破解现在leetcode已经超时了
var MyLinkedList = function() {
this.size = 0;
this.head = new ListNode(0);
};
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this.size) {
return -1;
}
let cur = this.head;
for (let i = 0; i <= index; i++) {
cur = cur.next;
}
return cur.val;
};
MyLinkedList.prototype.addAtHead = function(val) {
this.addAtIndex(0, val);
};
MyLinkedList.prototype.addAtTail = function(val) {
this.addAtIndex(this.size, val);
};
MyLinkedList.prototype.addAtIndex = function(index, val) {
if (index > this.size) {
return;
}
index = Math.max(0, index);
this.size++;
let pred = this.head;
for (let i = 0; i < index; i++) {
pred = pred.next;
}
let toAdd = new ListNode(val);
toAdd.next = pred.next;
pred.next = toAdd;
};
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this.size) {
return;
}
this.size--;
let pred = this.head;
for (let i = 0; i < index; i++) {
pred = pred.next;
}
pred.next = pred.next.next;
};
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
双向链表
需要定义的变量比较多的时候,往往考虑不全,需要多练习。
var MyLinkedList = function() {
this.size = 0;
this.head = new ListNode(0);
this.tail = new ListNode(0);
this.head.next = this.tail;
this.tail.prev = this.head;
};
MyLinkedList.prototype.get = function(index) {
if (index < 0 || index >= this.size) {
return -1;
}
let curr;
if (index + 1 < this.size - index) {
curr = this.head;
for (let i = 0; i <= index; i++) {
curr = curr.next;
}
} else {
curr = this.tail;
for (let i = 0; i < this.size - index; i++) {
curr = curr.prev;
}
}
return curr.val;
};
MyLinkedList.prototype.addAtHead = function(val) {
this.addAtIndex(0, val);
};
MyLinkedList.prototype.addAtTail = function(val) {
this.addAtIndex(this.size, val);
};
MyLinkedList.prototype.addAtIndex = function(index, val) {
if (index > this.size) {
return;
}
index = Math.max(0, index);
let pred, succ;
if (index < this.size - index) {
pred = this.head;
for (let i = 0; i < index; i++) {
pred = pred.next;
}
succ = pred.next;
} else {
succ = this.tail;
for (let i = 0; i < this.size - index; i++) {
succ = succ.prev;
}
pred = succ.prev;
}
this.size++;
const toAdd = new ListNode(val);
toAdd.prev = pred;
toAdd.next = succ;
pred.next = toAdd;
succ.prev = toAdd;
};
MyLinkedList.prototype.deleteAtIndex = function(index) {
if (index < 0 || index >= this.size) {
return;
}
let pred, succ;
if (index < this.size - index) {
pred = this.head;
for (let i = 0; i < index; i++) {
pred = pred.next;
}
succ = pred.next.next;
} else {
succ = this.tail;
for (let i = 0; i < this.size - index - 1; i++) {
succ = succ.prev;
}
pred = succ.prev.prev;
}
this.size--;
pred.next = succ;
succ.prev = pred;
};
function ListNode(val, next, prev) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
this.prev = (prev===undefined ? null : next)
}
206. 反转链表
双指针
var reverseList = function(head) {
let cur=head ;//第一个指针,初始值为第一个节点,先走
let pre=null;//初始值为null
let temp=null;
// const fummyHead= new ListNode(0);
while(cur){
temp=cur.next;//临时节点存储下一个节点,方便后续使用
cur.next=pre;//改变节点的指针方向
pre=cur;//节点继续走
cur=temp;//正常顺序的cur的下一个值
}
// return
return pre
};
递归
递归解法要复杂一点,之后要仔细研究一下
var reverseList = function(head) {
if (head == null || head.next == null) {
return head;
}
const newHead = reverseList(head.next);
head.next.next = head;
head.next = null;
return newHead;
};