JS链表系列

链表理论基础

什么是链表,链表是一种通过指针串联在一起的线性结构,每一个节由两部分组成,一个是数据域,一个是指针域(存放指向下一个节点的指针),最后一个节点的指针域指向null(空指针的意思)
链接的入口节点称为链表头节点也就是head
在这里插入图片描述

链表的类型:

1、单链表
单链表的指针域只能指向节点的下一个节点
2、双链表
双链表:每一个节点有两个指针域,一个指向下一个节点,一个指向上一个节点。
双链表即可以向前查询,也可以向后查询。
在这里插入图片描述
3、循环链表
循环链表,顾名思义,就是链表首尾相连。
循环链表可以用来解决约瑟夫环问题。
在这里插入图片描述

链表的存储方式

数组在内存中是连续分布的,但是链表在内存中可不是连续分布的。
链表是通过指针域的指针链接在内存中各个节点。
在这里插入图片描述

链表的操作

1、删除节点
在这里插入图片描述
在C++里最好是再手动释放这个D节点,释放这块内存,而在Java,Python,有自己的内存回收机制,就不用自己手动释放了。

2、添加节点
在这里插入图片描述

性能分析
在这里插入图片描述

JS链表代码
class ListNode{
    val;
    next = null;
    constructor(value){
        this.val = value;
        this.next = null;    
    }
}

1、移除链表元素

题目:删除链表中等于给定值 val 的所有节点。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
示例 2:
输入:head = [], val = 1
输出:[]

力扣题目链接:https://leetcode.cn/problems/remove-linked-list-elements/

思路:设置虚拟头节点,只需要将头节点往后移动一位就可以,实现从链表中移除了一个头节点

var removeElements = function(head, val) {
    // 初始化一个空节点,值为0,指向head
    let res = new ListNode(0,head);
    let cur = res; // 虚拟头节点

    while(cur.next){
        if(cur.next.val == val){
            cur.next = cur.next.next;
            continue;
        }
        cur = cur.next; // 往下走
    }
    return res.next;
};

补充知识:

new ListNode 常用方法:
1、初始化一个空节点,没有赋值,指针指向list
ListNode list = new ListNode();
2、初始化一个空节点,初始值为0,指针指向list
ListNode list = new ListNode(0);
3、初始化一个空节点,初始赋值为0,并且list的下一个next指针指向head,指针指向为list
ListNode list = new ListNode(0,head);
4、定义一个空链表
ListNode list = null;

2、设计链表

题目:在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

力扣题目链接:https://leetcode.cn/problems/design-linked-list/
思路:https://www.bilibili.com/video/BV1nB4y1i7eL

// 创建节点
class ListNode{
    constructor(val,next){
        this.val = val;
        this.next = next;
    }
}

var MyLinkedList = function() {
    this._size = 0; // 节点的数量
    this._head = null; // 头节点
    this._tail = null; // 尾节点
};

// 找到节点
MyLinkedList.prototype.getNode = function(index){
    // 判断索引index是否有效
    if(index<0 || index>=this._size) return null;
    // 创建虚拟头节点
    let node = new ListNode(0,this._head);
    // 找到以 index节点开头的链表
    while(index-- >= 0){
        node = node.next;
    }
    return node;
};

/** 
 * @param {number} index
 * @return {number}
 */
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) {
    let node = new ListNode(val,this._head);
    this._head =node; // 创建的新的节点作为头节点
    if(!this._tail){ // 如果就只有一个节点,那么头尾节点指向的都是同一个节点node
        this._tail = node;
    }
    this._size++; // 链表长度++
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    let node = new ListNode(val,null);
    if(this._tail){
        this._tail.next = node;
        this._tail =node;
        this._size++;
        return;
    }
    //链表只有一个值, 头节点与尾节点指向同一个节点node
    this._tail =node;
    this._head =node;
    this._size++;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    // 如果 index 等于链表的长度,则该节点将附加到链表的末尾。
    if(index == this._size){
        this.addAtTail(val);
        return;
    }
    
    // 如果 index 大于链表长度,则不会插入节点
    if(index > this._size) return;

    // 如果index小于0,则在头部插入节点。
    if(index <= 0){
        this.addAtHead(val);
        return;
    }

    let node = this.getNode(index-1); // 找到第index前一个节点
    node.next = new ListNode(val,node.next); // 创建一个节点,并且指向第index后一个节点,并用node钩住
    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;
            }
            // 获取目标节点的上一个的节点
            const node = this.getNode(index - 1);
            node.next = node.next.next; // 实现删除
            // 如果删除的是尾节点
            if (index === this._size - 1) {
                this._tail = node;
            }
            this._size--;
        };

3、反转链表

题目:反转一个单链表。
示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL

力扣题目链接:https://leetcode.cn/problems/reverse-linked-list/

var reverseList = function(head) {
    if(!head || !head.next) return head;
    let pre =null ;
    let tem = null;
    let cur = head;

    while(cur){
        tem = cur.next;
        cur.next = pre;
        pre = cur; // 这里的顺序不能错,必须是先移动pre
        cur = tem;                                         
        
    }

4、两两交换链表中的节点

题目:给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。
在这里插入图片描述

力扣题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/
思路:https://www.bilibili.com/video/BV1YT411g7br

在这里插入图片描述

var swapPairs = function(head) {
    let res = new ListNode(0,head); // 创建节点,并且指向head
    let temp = res;
    while(temp.next && temp.next.next){
        cur = temp.next.next; // 存放后一个节点
        pre = temp.next; // 存放前一个节点
        // 开始甩钩子
        pre.next = cur.next;
        cur.next = pre;
        temp.next = cur;
        temp = pre;
    }
    return res.next;
};

5、删除链表的倒数第N个节点

题目:给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
进阶:你能尝试使用一趟扫描实现吗?
在这里插入图片描述

力扣题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/

思路:双指针的经典应用,如果要删除倒数第n个节点,让cur移动n步,然后让cur,pre同时移动,直到cur指向链表末尾。删掉pre所指向的节点就可以了。

var removeNthFromEnd = function(head, n) {
    // 使用双指针
    let res = new ListNode(0,head); // 创建虚拟头节点
    let pre = res; // 前节点
    let cur = res; // 后节点
    while(n--) cur = cur.next; // cur 先移动n个节点
    while(cur.next){
        cur = cur.next;
        pre = pre.next;
    }
    pre.next = pre.next.next; // 删除节点

    return res.next;
};

6、链表相交

题目:给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null。
在这里插入图片描述
在这里插入图片描述

力扣题目链接:https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/

思路:这里其实判断是否尾部有指针相交,先要让两个链表尾部对齐,之后在判断链表值是否相等,有相等的就返回,没有就返回null。

     // 求链表长度
  var  getLength = function(head){
       // 如果是个空链表直接返回
       if(head == null) return head;
       let cur = head;
       var num = 0; // 计数
       while(cur){
           num++;
           cur = cur.next;
       }
       return num;
   }
var getIntersectionNode = function(headA, headB) {
    let curA = headA;
    let curB = headB;
    let lenA = getLength(headA);
    let lenB= getLength(headB);
 
    // 求出长度最长的哪个链表,固定用lenA,curA 存储起来
    if(lenA < lenB) {
        [curA,curB] = [curB,curA]; // 指针指向进行交换
        [lenA,lenB] = [lenB,lenA]; // 链表长度进行交换
    }

    let i = lenA-lenB;
    // 移动较长的链表,使其与较短的链表尾部对齐
    while(i--){
        curA = curA.next;
    }
    while(curA && curA != curB){
        curA = curA.next;
        curB = curB.next;
    }
    return curA;
};

7、环形链表

题目:给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。

力扣题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/
思路:https://www.bilibili.com/video/BV1if4y1d7ob

var detectCycle = function(head) {
    if(!head || !head.next) return null; 
    let slot = head.next; // 慢指针,每次走一个节点
    let fast = head.next.next; //快指针,每次走两个节点

    while(fast && fast.next && fast != slot){ // 判断链表是否存在环
        fast = fast.next.next;
        slot = slot.next;
    }

    // 链表没有环
    if(!fast || !fast.next) return null;
    // 链表有环,求环入口
    slot = head;
    while(slot != fast){
        slot = slot.next;
        fast = fast.next;
    }
    return fast; // 快节点与慢节点相遇,返回环入口
}
  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*neverGiveUp*

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值