算法学习day3 链表
链表是通过指针串联在一起的线性结构,每个节点包括数据和指针两个部分。当指针指向为null时表示空指针,说明这是最后一个节点。 链表的入口节点称为头节点head。
203.移除链表元素
203.移除链表元素
有两种方式:1.单独判断头节点是否移除,2.使用虚拟节点进行移除元素。
- 单独判断头节点是否需要移除
var removeElements = function(head, val) {
// 判断头节点
while(head !== null && head.val === val){
head = head.next
}
//用cur来进行判断,避免用head判断从而在return的时候head=null
let cur = head
//判断head指向的下一个节点是否需要移除,得先判断cur是否为null(可能出现之前的head被移除的情况)
while(cur !== null && cur.next !== null){
if(cur.next.val === val){
cur.next = cur.next.next
}else{
cur = cur.next
}
}
return head
};
- 使用虚拟节点进行移除元素
var removeElements = function(head, val) {
//设置虚拟头节点指向真实头节点,并用cur指向节点进行每次的判断
const dummyNode = new ListNode(null,head)
let cur = dummyNode
//判断当前指向的节点和它的next是否为空(即链表最后一个节点)
while(cur.next !== null){
if(cur.next.val === val){
cur.next = cur.next.next
}else{
cur = cur.next
}
}
//dummyNode指向的是head节点
return dummyNode.next
};
递归的方法还没思考清楚先放一下。。
707.设计链表
707.设计链表
不是很自信,先写下这次的理解(可能有些小错误)
var MyLinkedList = function() {
this._size = 0
this._head = null
this._tail = null
};
function ListNode(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
/**
* @param {number} index
* @return {number}
*/
MyLinkedList.prototype.get = function(index) {
//超过的index的判断
if(index < 0 || index >= this._size) return -1
//创建虚拟头节点
const dummyNode = new ListNode(null,this._head)
let cur = dummyNode
//循环直到找到index指向的节点
while(index >= 0){
cur = cur.next
index--
}
return cur.val
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtHead = function(val) {
// 创建要插入的节点
const newNode = new ListNode(val,this._head)
//修改头节点和链表尺寸
this._head = newNode
this._size ++
//如果原来是个空链表就会出现tail为空,也要进行tail的修改
if(!this._tail){
this._tail = newNode
}
};
/**
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtTail = function(val) {
const newNode = new ListNode(val,null)
this._size ++
//如果head和tail都不存在,即原为空链表,就都插入
if(!this._head && !this._tail){
this._head = newNode
this._tail = newNode
}else if(this._tail){
// 如果tail存在
// 先让原tail的指针指向改变为newNode
this._tail.next = newNode
// 在让最后一个元素_tail变为newNode
this._tail = newNode
}
};
/**
* @param {number} index
* @param {number} val
* @return {void}
*/
MyLinkedList.prototype.addAtIndex = function(index, val) {
//判断index超过size
if(index>this._size) return
//创建要插入的节点,只设置了val,没设置指针
const newNode = new ListNode(val)
//大于size的情况在尾部插入
if(index===this._size){
this.addAtTail(val)
}else if(index<=0){
//小于或等于0在头部插入
this.addAtTail(val)
}
//开始遍历查找要插入的index前的那一个节点
let cur = this._head
while(index-1 > 0){
cur = cur.next
index--
}
//插入节点
newNode.next = cur.next
cur.next = newNode
this._size++
};
/**
* @param {number} index
* @return {void}
*/
MyLinkedList.prototype.deleteAtIndex = function(index) {
//判断index
if(index<0||index>=this._size) return
//减小尺寸
this._size--
//索引为0,修改头节点
if(index === 0){
this._head = this._head.next
//若链表只有一个节点的情况,删除后tail也要修改
if(index===this._size-1){
this._tail = this._head
}
return
}
// 从head开始遍历,找到要删除的节点的前一位节点
let cur = this._head
while(index-1>0){
cur = cur.next
}
//删除节点
cur.next = cur.next.next
if(index === this._size-1){
this._tail = cur
}
};
206.反转链表
双指针
重点在指针向前走的先后顺序,还有何时保存和赋值指针指向的节点和next节点。
var reverseList = function(head) {
if(!head || !head.next) return head
// 两个指针,temp记录cur.next
let pre = null, cur = head,temp = null
while(cur){
// 第一次结果:temp=head.next
temp = cur.next
// 第一次结果:cur.next = null =>head.next=null
cur.next = pre
// 第一次结果:pre=head
pre = cur
//cur 向后走一位
cur = temp
}
return pre
};
递归
虽然没有完全理解递归,但是先记录一下这次的想法。
// head 在这里相当于之前双指针里的的cur
var reverseNode = function(pre,head){
// head 为null时结束,这时pre指向原链表的最后一个节点,return就相当于把这个节点当作头节点return出去(我的想法)
if(!head) return pre
const temp = head.next
head.next = pre
pre = head
return reverseNode(pre,temp)
}
var reverseList = function(head) {
return reverseNode(null,head)
}