代码随想录算法训练营第一天 | ● 203.移除链表元素 ● 707.设计链表 ● 206.反转链表

发布时间 2023-10-27 21:55:54作者: 李家成

今日学习的文章链接和视频链接

203.移除链表元素

var removeElements = function(head, val) {
    let node = new ListNode(null,head)
    let q = node.next;
    let p = node;
    while(q){
        if(q.val == val){
            p.next = q.next;
            q = p.next;
            continue;
        }
        p = q;
        q = q.next;
    }
    return node.next


};

● 707.设计链表

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){
    if(index<0||index >= this._size){
        return null
    }
    let cur = new LinkNode(0,this._head)
    while(index-->=0){
        cur = cur.next
    }
    return cur
}

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function(index) {
    if(index<0||index>=this._size){
        return -1
    }
    // let cur = this.getNode(index)
    // return cur.val
    return this.getNode(index).val
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    let node = new LinkNode(val,this._head);
    this._head = node;
    if(!this._tail){
        this._tail = node;
    }
    this._size++
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    if(this._size == 0){
        this.addAtHead(val)
        return 
    }
    let node = new LinkNode(val,null);
    this._tail.next = node;
    this._tail = node;
    this._size++
    // let node = new ListNode(val,null);
    // this._size++
    // if(this._tail){
    //     this._tail.next = node;
    //     this._tail = node;
    //     return 
    // }
    // this._tail = node;
    // this._head = node;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    if(index<0||index>this._size){
        return 
    }
    if(index == 0){
        this.addAtHead(val)
        return
    }
    if(index == this._size){
        this.addAtTail(val)
        return 
    }
    let pre = this.getNode(index-1);
    let node=new LinkNode(val,pre.next);
    pre.next = 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 
    }
    let node = this.getNode(index-1);
    node.next = node.next.next;
    if(index==this._size -1){
        this._tail = node;
    }
    this._size--

};

● 206.反转链表

var reverseList = function(head) {
    let pre = null;
    let cur = head;
    let temp = null
    while(cur !== null){
        temp = cur.next;
        cur.next = pre;
        pre = cur
        cur = temp;
    }
    return pre

};  

今日收获,记录一下自己的学习时长

4h