代码随想录算法训练营第三天|链表基本知识

虚拟头节点

文档链接: 代码随想录

LeetCode 203.Remove Linked List Elements

/**
 * 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) {

    const vNode = new ListNode(0, head)

    let slow = vNode
    let fast = vNode.next
    while (fast != null) {
        if (fast.val == val) {
            slow.next = fast.next
            fast = fast.next
        } else {
            slow = fast
            fast = fast.next
        }

    }
    return vNode.next
};

设计链表

非常有意义,对链表的结构和操作更加熟悉

文档链接:代码随想录

LeetCode 707.​​​​​​​Design Linked List

class LinkNode {
    constructor(val, next) {
        this.val = val;
        this.next = next;
    }
}
var MyLinkedList = function () {
    this._size = 0;
    this._tail = null;
    this._head = null;
};

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {

    if (index < 0 || index >= this._size) {
        return -1
    }
    let vhead = new LinkNode(0, this._head)
    while (index-- >= 0) {
        vhead = vhead.next
    }

    return vhead.val
};
MyLinkedList.prototype.getNode = function (index) {
    if (index < 0 || index >= this._size) {
        return -1
    }
    let vhead = new LinkNode(0, this._head)
    while (index-- >= 0) {
        vhead = vhead.next

    }
    return vhead
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
    const nHead = new LinkNode(val, this._head)
    this._size++
    this._head = nHead
    if (this._tail == null) {
        this._tail = nHead
    }
    return nHead

};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    const nTail = new LinkNode(val, null)
    if (this._tail != null) {
        this._tail.next = nTail
        this._tail = nTail
    } else {
        this._tail = nTail
        this._head = nTail
    }
    this._size++

};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
    if (index == this._size) {
        this.addAtTail(val)
    } else if (index > this._size || index < 0) {
        return
    } else if (index == 0) {
        this.addAtHead(val)
    } else {
        let preNode = this.getNode(index - 1)
        let nextNode = this.getNode(index)
        const addedNode = new LinkNode(val, nextNode)
        preNode.next = addedNode
        this._size++
    }

};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    console.log('size', this._size)
    let vhead = new LinkNode(0, this._head)
    while (vhead != null) {
        console.log('index-->', vhead.val)
        vhead = vhead.next
    }
    if (index < 0 || index >= this._size) {
        return
    }

    if (index == 0) {

        if (this._size == 1) {
   
            this._head = null
            this._tail = null
        } else {
            this._head = this.getNode(1)
    
        }

    } else {
        const node = this.getNode(index - 1)
        node.next = node.next.next
        if (node.next == null) {
            this._tail = node
        }
    }
    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)
 */

反转链表

 文档链接: ​​​​​​​代码随想录

 LeetCode 206.​​​​​​​Reverse Linked List

/**
 * 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 fast = head
    let slow = null
    while (fast != null) {
        let temp = fast.next
        fast.next = slow
        slow = fast
        fast = temp
    }
    return slow
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值