代码随想录刷题笔记Day3--链表之移除链表元素 设计链表 反转链表

代码随想录刷题笔记Day3–链表之移除链表元素 设计链表 反转链表

LeetCode 203. 移除链表元素

题目描述:

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点

示例 1:

输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]

示例 2:

输入:head = [], val = 1
输出:[]

解题思路

  • 如果是在中间发现了目标数值,那么就把指针指向目标值指针的下一个指针
  • 在这里插入图片描述
  • 但是存在一种情况,就i是在头节点就发现了目标数值,则处理方法前者不同,head=head->next;
  • 在这里插入图片描述
  • 在上面那种情况需要使用while循环,切记!
    在这里插入图片描述

解法:

第一种写法(本人写法)

var removeElements = function(head, val) {
    while(head!=null&&head.val===val)
    head=head.next;
    let t=head;
    while(t!=null&&t.next!=null)
    {
       if(t.next.val===val)
       {
           t.next=t.next.next;
       }
       else  t=t.next;
    }
    return head;
};

反思:

  1. 头节点的情况没考虑到
  2. 头节点仍然需要循环,因为可能存在上图所示的情况(考虑问题不够全面)
  3. t需要从头开始

第二种写法(虚拟头节点)

var removeElements = function(head, val) {
  let dummyhead=new ListNode();
  dummyhead.next=head;
  let cur=dummyhead;
  while(cur.next!=null)
      {
          if(cur.next.val===val)
              cur.next=cur.next.next;
          else 
              cur=cur.next;
      }
  return dummyhead.next;
};

反思:

  1. cur需要从头开始
  2. return的是虚拟头节点的下一个节点

LeetCode 707. 设计链表

题目描述:

设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:valnextval 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

示例 :

MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2);   //链表变为1-> 2-> 3
linkedList.get(1);            //返回2
linkedList.deleteAtIndex(1);  //现在链表是1-> 3
linkedList.get(1);            //返回3

解法

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

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

/** 
 * @param {number} index
 * @return {number}
 */
MyLinkedList.prototype.get = function (index) {
    //如果索引大于链表长度
    if (index > this.length - 1) {
        return -1;
    }
    let ptr = this.head;
    while (index--) {
        ptr = ptr.next;
    }
    return ptr.val;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtHead = function (val) {
    //建立新增节点
    let node = new LinkNode(val, this.head);
    this.head = node;
    //当链表中无节点时
    if (this.length === 0) {
        this.tail = node;
    }
    this.length++;
};

/** 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtTail = function (val) {
    //建立新增节点
    let node = new LinkNode(val, null);
    //当链表中无节点时
    if (this.length === 0) {
        this.head = node;
    }
    //有节点时
    else {
        this.tail.next = node;
    }
    this.tail = node;
    this.length++;
};

/** 
 * @param {number} index 
 * @param {number} val
 * @return {void}
 */
MyLinkedList.prototype.addAtIndex = function (index, val) {
    //如果索引大于链表长度
    if (index > this.length) {
        return;
    }
    //当在头部添加时
    if (index === 0) {
        //直接调用
        this.addAtHead(val);
        return;
    }
    //在尾部添加时
    if (index === this.length) {
        this.addAtTail(val);
        return;
    }
    //其他情况
    //ptr指向插入位置的前一位
    index = index - 1;
    let ptr = this.head;
    while (index--) {
        ptr = ptr.next;
    }
    // 新建节点,让它指向插入位置的值
    let node = new LinkNode(val, ptr.next);
    ptr.next = node;

    this.length++;
};

/** 
 * @param {number} index
 * @return {void}
 */
MyLinkedList.prototype.deleteAtIndex = function (index) {
    //如果索引大于链表长度
    if (index > this.length - 1 || index < 0) {
        return;
    }
    // 如果index为0,head向后移动一位
    if (index === 0) {
        this.head = this.head.next;
        //如果删除的也是尾节点
        if (index === this.length - 1) {
            this.tail = this.head;
        }
        this.length--;
        return;
    }

    let id = index;
    //ptr指向插入位置的前一位
    index = index - 1;
    let ptr = this.head;
    while (index--) {
        ptr = ptr.next;
    }
    ptr.next = ptr.next.next;
    //删除最后一位
    if (id === this.length - 1) {
        this.tail = ptr;
    }

    this.length--;
};

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

题目描述:

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

示例 :

输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]

解法

第一种写法(双指针写法)

/**
 * 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) {
     const pre = new ListNode(); 
      let cur=head;
      while(cur!=null)
      {
         let temp=cur.next;
         pre=cur.next
         pre=cur;
         cur=temp;
      }
      return pre;
};

反思:

一定需要

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值