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

203.移除链表元素

建议: 本题最关键是要理解 虚拟头结点的使用技巧,这个对链表题目很重要。
题目链接/文章讲解/视频讲解::https://programmercarl.com/0203.移除链表元素.html

/**
 * 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 first = new ListNode(null, head);
    let cur = first;
    while(first.next){
        if (first.next.val===val) {
            first.next = first.next.next;
        } else {
            first = first.next;
        }
    }
    return cur.next;
};

707.设计链表

建议: 这是一道考察 链表综合操作的题目,不算容易,可以练一练 使用虚拟头结点
题目链接/文章讲解/视频讲解:https://programmercarl.com/0707.设计链表.html

class LinkNode{
    constructor(val, next = null){
        this.val = val;
        this.next =next;
    }
}


var MyLinkedList = function() {
    this.head = null
    this.size = 0;
};

MyLinkedList.prototype.getNode = function(index) {
    if(index>=this.size){
        return -1;
    }
    let count = 0;
    let cur = this.head;
    while(count<index){
        cur = cur.next;
        count++;
    }
    return cur;
};

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function(index) {
    let cur = this.getNode(index);
    return cur===-1?-1:cur.val;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function(val) {
    let cur = this.head;
    let node = new LinkNode(val,cur);
    this.head = node;
    this.size++;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function(val) {
    let node = new LinkNode(val,null);
    if (this.size===0) {
        this.head = node;   
    } else {
        let cur = this.head;
        while(cur.next){
            cur = cur.next;
        }
        cur.next = node;
    }
    this.size++;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function(index, val) {
    if(index>this.size){
        return;
    }
    this.size++;
    if(index<=0){
        this.addAtHead(val);
        return;
    }

    if(index>=this.size){
        this.addAtTail(val);
        return;
    }

    const prevnode = this.getNode(index-1);
    let node = new LinkNode(val);
    node.next = prevnode.next;
    prevnode.next = node;

};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function(index) {
    if(index>=this.size){
        return;
    }
    this.size--;
    if(index===0){
        this.head = this.head.next;
        return;
    }
    let node = this.getNode(index-1);
    node.next = node.next.next;
};

/**
 * 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.反转链表

建议先看我的视频讲解,视频讲解中对 反转链表需要注意的点讲的很清晰了,看完之后大家的疑惑基本都解决了。
题目链接/文章讲解/视频讲解:https://programmercarl.com/0206.翻转链表.html

/**
 * 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) {
    if (head==null) {
        return head;
    }
    let prev = null;
    let cur = head;
    while(cur&&cur.next){
        let last = cur.next;
        cur.next = prev;
        prev = cur;
        cur = last;
    }
    cur.next = prev;
    return cur;
};
posted @ 2024-05-10 23:31  YuanYF6  阅读(2)  评论(0编辑  收藏  举报