链表算法总结一(JavaScript版)

203. 移除链表元素 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} val
 * @return {ListNode}
 */
var removeElements = function (head, val) {
    let dummyHead = new ListNode(0)
    dummyHead.next = head
    let cur = dummyHead
    while (head) {
        if (head.val === val) {
            cur.next = head.next
        } else {
            cur = cur.next
        }
        head = head.next
    }
    return dummyHead.next
};

707. 设计链表 - 力扣(LeetCode)

function Node(val) {
    this.val = val
    this.next = null
}
var MyLinkedList = function () {
    this.dummyHead = new Node(0)
    this.size = 0
};

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {
    if (index < 0 || index >= this.size) {
        return -1
    }
    let cur = this.dummyHead
    for (let i = 0; i <= index; i++) {
        cur = cur.next
    }
    return cur.val
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
    this.addAtIndex(0, val)
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    this.addAtIndex(this.size, val)
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
    if (index < 0 || index > this.size) {
        return
    }
    // 找到插入位置的前面的节点
    let pre = this.dummyHead
    for (let i = 0; i < index; i++) {
        pre = pre.next
    }
    // 插入
    const node = new Node(val)
    node.next = pre.next
    pre.next = node
    this.size++
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    if (index < 0 || index >= this.size) {
        return
    }
    let pre = this.dummyHead
    for (let i = 0; i < index; i++) {
        pre = pre.next
    }
    pre.next = pre.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. 反转链表 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var reverseList = function (head) {
    let pre = null
    while (head) {
        const cur = head
        head = head.next
        cur.next = pre
        pre = cur
    }
    return pre
};

24. 两两交换链表中的节点 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var swapPairs = function (head) {
    // 这道题画画图就容易出来了
    let dummyHead = new ListNode(0)
    dummyHead.next = head
    let fast = dummyHead
    while (fast.next && fast.next.next) {
        let cur = fast.next
        fast.next = cur.next
        cur.next = cur.next.next
        fast.next.next = cur
        fast = cur
    }
    return dummyHead.next
};

19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @param {number} n
 * @return {ListNode}
 */
var removeNthFromEnd = function (head, n) {
    let dummyHead = new ListNode(0)
    dummyHead.next = head
    let fast = dummyHead, slow = dummyHead
    // 先让一个快指针走
    for (let i = 0; i < n + 1; i++) {
        fast = fast.next
    }
    while (fast) {
        fast = fast.next
        slow = slow.next
    }
    slow.next = slow.next.next
    return dummyHead.next
};            

面试题 02.07. 链表相交 - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} headA
 * @param {ListNode} headB
 * @return {ListNode}
 */
var getIntersectionNode = function (headA, headB) {
    let lenA = 0, lenB = 0
    let curA = headA, curB = headB
    while (curA) {
        curA = curA.next
        lenA++
    }
    while (curB) {
        curB = curB.next
        lenB++
    }
    let curL = lenA > lenB ? headA : headB
    let curS = curL === headA ? headB : headA
    const steps = Math.abs(lenA - lenB)
    for (let i = 0; i < steps; i++) {
        curL = curL.next
    }
    while (curL) {
        if (curL === curS) {
            return curL
        }
        curL = curL.next
        curS = curS.next
    }
    return null
};

142. 环形链表 II - 力扣(LeetCode)

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
var detectCycle = function (head) {
    // 快慢指针
    let fast = head, slow = head
    while (fast && fast.next) {
        fast = fast.next.next
        slow = slow.next
        if (fast === slow) {
            break
        }
    }
    if (!fast || !fast.next) {
        return null
    }
    let cur = head
    while (cur !== slow) {
        cur = cur.next
        slow = slow.next
    }
    return cur
};

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值