• 【LeetCode与《代码随想录》】链表篇:做题笔记与总结-JavaScript版


    代码随想录

    代码随想录
    代码随想录CSDN官方

    203. 移除链表元素

    203. 移除链表元素

    使用虚拟头结点。

    var removeElements = function (head, val) {
        // 头结点:下一个是head
        const ans = new ListNode(0, head)
        let now = ans;
        // 从头结点开始遍历
        while (now.next) {
            if (now.next.val === val) {
                // 跳过当前节点
                now.next = now.next.next;
                continue;
            }
            // 指针后移
            now = now.next;
        }
        return ans.next;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    707. 设计链表

    707. 设计链表

    • 细节很多的链表题
    • 建议封装“获取某个节点”的方法
    • 注意头尾细节:
    • 对于addAtHead,如果链表为空,头节点也是尾节点
    • 对于addAtTail,如果链表为空,尾节点也是头节点
    • 对于deleteAtIndex,如果索引为0,要改变this._head,如果索引为最后一个节点,要改变this._tail
    • 注意null没有next
    • 每次改变头尾节点时要改变this._tailthis._head
    • 改变长度时要改变this._size
    • 链表题一般可以用虚拟头节点
    // 创建节点
    class LinkNode {
        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) {
        // 虚拟头结点,下一个是head
        let now = new LinkNode(0, this._head);
        while (index >= 0) {
            now = now.next;
            index--;
        }
        return now;
    }
    
    /** 
     * @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 newHead = new LinkNode(val, this._head);
        this._head = newHead;
        this._size++;
        // 特判:若原链表为空,头就是尾
        if (!this._tail) {
            this._tail = newHead;
        }
    };
    
    /** 
     * @param {number} val
     * @return {void}
     */
    MyLinkedList.prototype.addAtTail = function (val) {
        let newTail = new LinkNode(val, null);
        this._size++;
        // 若链表为空,头是尾
        if (!this._tail) {
            this._head = newTail;
            this._tail = newTail;
        } else {
            this._tail.next = newTail;
            this._tail = newTail;
        }
    
    };
    
    /** 
     * @param {number} index 
     * @param {number} val
     * @return {void}
     */
    MyLinkedList.prototype.addAtIndex = function (index, val) {
        if (index <= 0) {
            this.addAtHead(val);
        } else if (index > this._size) {
            return;
        } else if (index === this._size) {
            this.addAtTail(val)
        } else {
            let pre = this.getNode(index - 1);
            let newNode = new LinkNode(val, pre.next);
            pre.next = newNode;
            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;
        } else if (index === this._size - 1) {
            // 删最后一个
            let pre = this.getNode(index - 1);
            pre.next = null;
            this._tail = pre;
        } else {
            let pre = this.getNode(index - 1);
            pre.next = pre.next.next;
        }
        this._size--;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    206. 反转链表(双指针)

    206. 反转链表

    • 双指针
    • 注意[][1]的情况
    • 反转后head.nextnull,否则会有循环节点
    • 结束的条件:b指针为空,注意,在b===null的上一轮c已经为null,则它没有next
    var reverseList = function (head) {
        if (head === null || head.next === null) return head;
        let a = head, b = head.next, c = b.next;
        a.next = null;
        while (b) {
            b.next = a;
            a = b;
            b = c;
            if (c) c = c.next;
        }
        return a;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    24. 两两交换链表中的节点(双指针)

    24. 两两交换链表中的节点

    思路:
    在这里插入图片描述

    • 双指针
    • 如果是奇数的话,那么只有前偶数个才会互换,因此跳出循环的条件是b、c均不为null
    • 若是奇数,则最后的b为null,无next,要特判一下
    • 建议用虚拟头节点
    var swapPairs = function (head) {
        if (!head || !head.next) return head;
        let newPre = new ListNode(0, head), a = newPre;
        let b = a.next, c = b.next;
        while (b && c) {
            b.next = c.next;
            a.next = c;
            c.next = b;
            a = b;
            b = b.next;
            if (b) c = b.next;
        }
        return newPre.next;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    19. 删除链表的倒数第 N 个结点(双指针)

    19. 删除链表的倒数第 N 个结点

    • 双指针:快慢指针
    var removeNthFromEnd = function (head, n) {
        let newPre = new ListNode(0, head);
        let now1 = newPre, now2 = now1;
        for (let i = 0; i < n; i++) {
            now1 = now1.next;
        }
        while (now1.next) {
            now1 = now1.next;
            now2 = now2.next;
        }
        // now1为尾节点
        now2.next = now2.next.next;
        return newPre.next;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    面试题 02.07. 链表相交

    面试题 02.07. 链表相交

    思路:

    在这里插入图片描述

    • 计算链表长度,长的先走几步,直到它们从起始位置到终止位置长度相同
    var getIntersectionNode = function (headA, headB) {
        let a = 0, b = 0;
        let pa = headA, pb = headB;
        while (pa) {
            a++; pa = pa.next;
        }
        while (pb) {
            b++; pb = pb.next;
        }
    
        pa = headA, pb = headB;
        if (a < b) {
            while (a < b) {
                pb = pb.next; b--;
            }
        } else if (a > b) {
            while (a > b) {
                pa = pa.next; a--;
            }
        }
    
        while (pa != pb && pa && pb) {
            pa = pa.next;
            pb = pb.next;
        }
    
        if (pa) return pa;
        else return null;
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    142. 环形链表 II(双指针)

    142. 环形链表 II

    • 双指针:快慢指针ij
    • 追及问题:快指针j一次两步,慢指针i一次一步,若有环则j会刚好在比i多一圈时追上i
    • 若无环,快指针会先到null,若有环,永远不可能到null(因此不能计数)
    • 如何计算环入口:在相遇时,在head处新加入指针k,一起向前,会在环入口处相遇,证明在下文

    动图(来自代码随想录):

    在这里插入图片描述
    数学推导:

    设头结点到环入口处为x,环入口到环尾为y,则环的长度为y+1。设从入口到相遇的点的距离为z,则有:

    慢指针:x+z
    快指针:x+z+y+1

    ps:快指针一次两步,慢指针一次一步,则快指针在相遇时会比慢指针快一圈(追及问题),且快指针的长度是慢指针的两倍:

    则2(x+z)=x+z+y+1,即x+z=y+1

    从相遇处到入口的距离为:y+1-z(环的长度减去入口到相遇位置),即x,而x就是我们要求的头结点到环入口的长度。证毕

    var detectCycle = function (head) {
        let i = head, j = head;
    
        while (i && j) {
            i = i.next;
            j = j.next;
            if (j) j = j.next;
            if (i === j) break;
        }
    
        // 有环
        if (i === j && i && j) {
            // 这里相遇
            let k = head;
            while (k !== i) {
                k = k.next;
                i = i.next;
            }
            return k;
        } else {
            // 无环
            return null;
        }
    
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
  • 相关阅读:
    《HTML+CSS+JavaScript》之第22章 浮动布局
    Java包装类
    Java基础知识(Day2)
    【iOS】—— autoreleasepool详解
    计算机专业毕业设计项目推荐08-英语在线点读平台(SpringBoot+Vue+MongoDB)
    没有经验能成为产品经理吗?
    找准边界,吃定安全 | 从访问控制谈起,再看零信任模型
    前端css 纯数字或者字母 溢出不换行
    计算机毕业设计springboot+vue基本微信小程序的学生健康管理小程序
    kafka学习笔记04(小滴课堂)
  • 原文地址:https://blog.csdn.net/karshey/article/details/127873942