学习目标:
● 链表理论基础
● 203.移除链表元素
● 707.设计链表
● 206.反转链表
学习内容:
链表的存储方式:数组是在内存中是连续分布的,但是链表在内存中可不是连续分布的。
方法:虚拟头结点(操作统一)
//
class ListNode {
val;
next = null;
constructor(value) {
this.val = value;
this.next = null;
}
}
学习产出:
一:203.移除链表元素
题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.%E7%A7%BB%E9%99%A4%E9%93%BE%E8%A1%A8%E5%85%83%E7%B4%A0.html
二:
//第一次打卡出现问题
let res = new ListNode(0,head)//虚拟头结点
//cur.next.val 取值 == if,else逻辑
if(cur.next.val == val){
cur.next = cur.next.next
}else{
cur = cur.next
}
三:
var removeElements = function(head, val) {
let res = new ListNode(0,head)
let cur = res
while(cur.next){
if(cur.next.val == val){
cur.next = cur.next.next
}else{
cur = cur.next
}
}
return res.next
};
学习产出:
一: 707.设计链表
题目链接/文章讲解/视频讲解:https://programmercarl.com/0707.%E8%AE%BE%E8%AE%A1%E9%93%BE%E8%A1%A8.html
二:
连看带抄写完了,还是有点懵!!!!
三:
class LinkNode {
constructor(val, next) {
this.val = val;
this.next = next;
}
}
/**
* Initialize your data structure here.
* 单链表 储存头尾节点 和 节点数量
*/
var MyLinkedList = function() {
this._size = 0;
this._head = null;
};
/**
* Get the value of the index-th node in the linked list. If the index is invalid, return -1.
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.getNode = function(index) {
if(index < 0 || index >= this._size) return null;
// 创建虚拟头节点
let cur = new LinkNode(0, this._head);
// 0 -> head
while(index-- >= 0) {
cur = cur.next;
}
return cur;
};
MyLinkedList.prototype.get = function(index) {
if(index < 0 || index >= this._size) return -1;
// 获取当前节点
const node = this.getNode(index);
return node ? node.val : -1;
};
/**
* 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.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
const node = new LinkNode(val, this._head);
this._head = node;
this._size++;
};
/**
* Append a node of value val to the last element of the linked list.
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
const node = new LinkNode(val, null);
if (this._size === 0) {
this._head = node
} else {
let cur = this._head
while(cur.next != null){
cur = cur.next
}
cur.next = node
}
this._size++;
};
/**
* 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.
* @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;
}
// 获取目标节点的上一个的节点
const node = this.getNode(index - 1);
node.next = new LinkNode(val, node.next);
this._size++;
};
/**
* Delete the index-th node in the linked list, if the index is valid.
* @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;
return
}
// 获取目标节点的上一个的节点
const node = this.getNode(index - 1);
node.next = node.next.next;
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.反转链表
题目链接/文章讲解/视频讲解:https://programmercarl.com/0206.%E7%BF%BB%E8%BD%AC%E9%93%BE%E8%A1%A8.html
二.
双指针第一遍打卡问题
let temp = null //提到while外
pre = cur
cur= temp //写成pre = cur.next???
//双指针
var reverseList = function(head) {
let cur = head
let pre = null
let temp = null
while(cur){
temp = cur.next
cur.next = pre
pre = cur
cur = temp
}
return pre
};
三.
递归忘记写return
var reverseList = function(head) {
return reverse(null,head)
}
function reverse(pre,cur){
if(cur == null) return pre
let temp = cur.next
cur.next = pre
return reverse(cur,temp)
}