【链表专题】剑指 Offer 18. 删除链表的节点

本问题对应课程链接:剑指offer刷题第五期

要求:

1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路。
2、给出你代码所用的时间复杂度和空间复杂度

共以下 54 个回答

  • mpweixin用户 普通 2024年5月14日 上午11:54

    打卡成功

    0 赞同 0 条回复
  • 刘成志 永久会员 2023年10月8日 下午7:38

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode
    deleteNode(ListNode* head, int val) {
    ListNode* ret = head;
    if(head->val val)
    {
    return head->next;
    }
    while ((ret->next != nullptr)&&(ret->next->val != val))
    {
    ret = ret->next;
    }
    if (ret->next != nullptr)
    {
    ret->next = ret->next->next;
    }
    return head;
    }
    };
    遍历一遍链表,找到目标节点,失当前节点指向目标节点下一个节点即可。需注意头节点也也可能是目标节点的情况。
    时间复杂度为 O(n),空间复杂度为O(1).

    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年8月28日 下午11:27

    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if (head->val val) {
    return head->next;
    }
    ListNode* last = head;
    ListNode* t = head->next;
    while (t != NULL) {
    if (t->val
    val){
    last->next = t->next;
    }
    t = t->next;
    last = last->next;
    }
    return head;
    }
    };
    感觉这道题还蛮简单,没想到还记得怎么写hhh

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月22日 下午5:27

    定位节点,修改引用,注意头结点和空节点
    class Solution:
    def deleteNode(self, head:ListNode, val: int):
    if head.val val:
    return head.next
    pre, cur = head, head.next
    while cur and cur.val != val:
    pre, cur = cur, cur.next
    if cur:
    pre.next = cur.next
    return head

    0 赞同 0 条回复
  • Jmin 普通 2023年8月22日 下午2:12

    1.采用前后双指针思路,主要是要考虑边界问题,当head为null或者head.val = val的情况
    2.通过while循环,找到值之后终止掉,最后return整个链表

    /**
     * @param {ListNode} head
     * @param {number} val
     * @return {ListNode}
     */
    var deleteNode = function(head, val) {
        if (head === null || head.val === val) {
            head = head.next
        }
        let pre = head
        let current = head.next
        while (current) {
            if (current.val === val) {
                pre.next = current.next
                break
            }
            pre = current
            current = current.next
        }
    
        return head
    
    };
    
    0 赞同 0 条回复
  • 我一定要坚持打卡 普通 2023年8月22日 上午11:39

    这是第一题打卡
    总结下踩的坑
    1. 好久没写java,自己写测试函数,都忘记写类的static修饰
    2. 用while循环判定的时候,一开始想把找val值和判定为空写在一块,就判定node节点是否为空一直写不出来,后边换了个环境,脑子清晰了之后就写出来了
    3. 使用java写判定的时候,因为对head节点单独处理,这里直接用了head.equals去判定,而没用val判定,自己测试的时候导致删除头结点失败,后边debug的时候找到了问题所在

    package day1;
    
    import java.util.Iterator;
    import java.util.LinkedList;
    
    public class Offer18删除链表的节点 {
            
        
    /**
     * 下边两段注释的代码因为没用static静态修饰,在静态的main主函数中调用会非法    
     * @author Lenovo
     *
     */
    //  public class ListNode {
    //      int val;
    //      ListNode next;
    //      ListNode(int x) { 
    //        val = x; 
    //    }
    //  }
        
    //  public  ListNode deleteNode(ListNode head, int val) {
    //        if(head==null){
    //            return head;
    //        }
    //      if(head.val==val) {
    //          head=head.next;
    //            return head;
    //      }
    //        // 下边就处理不是空链表和val不是头结点的情况
    //        // 删除指定值的节点 先用first指针找到待删除的前一个节点
    //        // 再将前一个指针指向待删除节点的后一个节点即可
    //      ListNode first=head;
    //      ListNode node= first.next;
    //      
    //      while (node!=null) {//这里while条件里边只需要判定循环结束,不参与找值的过程
    //
    //          if (node.val==val) {//在if中进行找值
    //              first.next=node.next;           
    //          }
    //          first=node;
    //          node=node.next;
    //          
    //      }
    //      if (node==null) {
    //          System.out.println("不存在要删除的节点值val");
    //          System.out.println("下边是未经处理的链表值");
    //      }
    //      return head;
    //  }
        
        public static class ListNode {
            int val;//存储元素
            ListNode next;
            public ListNode(int x){
                val = x;
            }
            @Override
            public String toString() {
                // TODO Auto-generated method stub
                StringBuilder stringBuilder = new StringBuilder();
                
                return "value:"+val;
            }
        }
    
        
    //  public static ListNode deleteNode(ListNode head, int val) {
    //      if(head.equals(val)) {
    //          head=head.next;
    //      }
    //      ListNode preListNode=head;   
    //      ListNode next= node.next;
    //      
    //      while (!node.equals(null)&&!next.equals(null)) {
    //          if (next.val==val) {
    //              node.next=next.next;
    //                          
    //          }
    //          node=next;
    //          next=next.next;
    //          
    //      }       
    //      return head;
    //  }
        
        public static ListNode deleteNode(ListNode head, int val) {
            if(head==null){
                return head;
            }
            if(head.val==val) {
                head=head.next;
                return head;
            }
            // 下边就处理不是空链表和val不是头结点的情况
            // 删除指定值的节点 先用first指针找到待删除的前一个节点
            // 再将前一个指针指向待删除节点的后一个节点即可
            ListNode first=head;
            ListNode node= first.next;
            
            while (node!=null) {//这里while条件里边只需要判定循环结束,不参与找值的过程
    
                if (node.val==val) {//在if中进行找值
                    first.next=node.next;           
                }
                first=node;
                node=node.next;
                
            }
            if (node==null) {
                System.out.println("不存在要删除的节点值val");
                System.out.println("下边是未经处理的链表值");
            }
            return head;
        }
        
        
        
        public static void main(String[] args) {
    //      这里测试用例 先对list列表赋值一个,再用循环进行插入,不然下边不方便操作
            ListNode list  =new ListNode(0);
    //      ListNode list= null;
            ListNode temp= list;
            for (int i = 1; i < 10; i++) {
                temp.next=new ListNode(i);
                temp=temp.next;
            }
            ListNode temp1=deleteNode(list,10);
            
            while (temp1!=null) {
                System.out.println(temp1);
                temp1=temp1.next;
                
            }
            
        }
    
    
    }
    

    时间复杂度O(n)
    空间复杂度O(1)
    这里在自己想思路的时候,因为有点混乱,所以一直卡住了,要先判定hed是否为空,不然后边对head的操作都有可能会操作空节点;其次要判定头结点是否是要删除的值来做特殊处理,因为这里给的单链表不是那种带特殊头结点的那种,不能像链表中的节点一样处理,所以要单独处理;最后可以将不管val为null,还是为删除的是链表中的节点都可以统一处理,这个思路是最正确的。

    1 赞同 0 条回复
  • 黎明之前 普通 2023年8月21日 上午8:31

    //检测头节点是否为空 返回空
    //检测头节点是否为值是否为所设的值 返回节点
    //设当前的指针和前指针 遍历整个 替换整个链表 找寻值相同的
    java
    if(head NULL)
    {
    return NULL;
    }
    if(head.val
    val)
    {
    return head.next;
    }
    LIstNode temp = head.next;
    LIstNode pre = head;
    while(temp !=NULL)//遍历可以从头到尾 也可以判断值是否正确while(temp!=NULL&&temp.valval)
    {
    if(temp.val
    val)
    {
    pre.next=temp.next;//删除节点之后的
    return head;
    }
    temp= temp.next;
    pre=pre.next;

    }
    c++
    if(head->val val)
    {
    return head->next;
    }
    ListNode *pre = head;
    LIstNode *cur = head.next;
    while(cur != NULL&&cur ->val != val)//指针的传递有两种1 直接移入下一个 或者换成另一个
    {
    pre = cur;
    cur ->cur->next;
    }
    if(cur !=NULL)
    {
    pre->next = cur ->next;//截节点位置
    }
    return head;

    0 赞同 0 条回复
  • Serena 普通 2023年8月19日 下午2:28
    //先判断头结点是不是要删除的节点
            if(head.val==val){
                return head.next;
            }
            //头结点不是要删除的节点
            ListNode temp2=head.next;
            ListNode temp1=head;
            for(;temp2!=null;temp2=temp2.next){
                if(temp2.val==val){
                    temp1.next=temp2.next;
                    return head;
                }
                temp1=temp1.next;
            }
            return null;
    
    0 赞同 0 条回复
  • Di questa storia sempre a lie to 普通 2023年8月17日 下午8:22

    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if(head.val val) return head;
    ListNode *pre=head, *cur=head->next;
    while(cur !=nullptr &cur->val!=val)
    {
    pre = cur;
    cur = cur->next;
    }
    if(cur != nulptr) pre->next = cur->next;
    return head;
    }

    时间复杂度:O(N)
    空间复杂度:O(1)

    0 赞同 0 条回复
  • 梧葳 普通 2023年8月17日 上午1:00

    采用双指针方式,若curval,直接pre->next=cur->next即可删除指定的节点。
    先用C,下回补个java。

    struct ListNode* deleteNode(struct ListNode* head, int val){
    // 判空
    if (head NULL) {
    return NULL;
    }
    // 头判断
    if (head->val val) {
    head = head->next;
    return head;
    }
    // 双指针遍历
    struct ListNode *cur = head;
    struct ListNode *pre = NULL;
    while (cur->next != NULL) {
    pre = cur;
    cur = cur->next;
    if (cur->val
    val) {
    pre->next = cur->next;
    return head;
    }
    }
    // 返回
    return head;
    }

    0 赞同 0 条回复
  • 西西弗斯的石头 永久会员 2023年8月16日 下午10:40

    时间复杂度O(n),空间复杂度O(1).。

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head == null||head.next == null){
                return null;
            }
            if(head.val == val) return head.next;
            ListNode temp = head;
            ListNode i = null;
            while(temp.val != val){
                i = temp;
                temp = temp.next;
            }
            i.next = temp.next;
            temp.next = null;
            return head;
        }
    }
    
    1 赞同 0 条回复
  • 海浪漂哇漂 普通 2023年8月16日 下午7:56

    时间复杂度O(n)
    空间复杂度O(1)
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {

        if(head == null){
            return head;
        }
        if(head.val == val){
            return head.next;
        }
        ListNode temp = head;
        while(head.next != null){
            if(head.next.val == val){
                head.next = head.next.next;
                break;
            }
            head = head.next;
        }
        return temp;
    
    }
    

    }

    1 赞同 0 条回复
  • Miracle· 普通 2023年8月15日 下午9:21

    事件复杂度:o(n),空间复杂度o(1)
    思路:维护一个先前节点指针和当前节点指针,判断当前节点值是否等于要删除值,等于的话讲先前节点下一节点指向当前节点下一节点,要注意判断首节点是要删除的节点的情况。
    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode
    deleteNode(ListNode* head, int val) {
    ListNode *cur, *per;
    if (head->val val) {
    return head->next;
    }
    per = head;
    cur = head->next;
    while (cur != nullptr && cur->val != val) {
    per = cur;
    cur = cur->next;
    }
    if (cur != nullptr) per->next = cur->next;
    return head;
    }
    };

    0 赞同 0 条回复
  • Sheng 永久会员 2023年8月15日 下午5:43

    ”’java
    时间复杂度O(n)空间复杂度O(1)
    思路:处理好空节点和首节点即可
    //单链表删除节点
    public class Solution {
    public ListNode deleteNode(ListNode head, int val)
    {
    ListNode node = head;
    if(head.val val) {
    head = head.next;
    return head;
    }

        while(node.next != null) {
            if(node.next.val == val) {
                if(node.next.next != null) {
                    node.next = node.next.next;
                }
                else {
                    node.next = null;
                }
                break;
            }
            else {
                node = node.next;
            }
        }
        return head;
    }
    

    }

    0 赞同 0 条回复
  • sc_Yang 普通 2023年8月15日 下午4:26

    时间复杂度:o(m),空间复杂度o(1)
    先判断头结点;
    再判断后续节点;

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            //处理头结点
            if(head->val == val){
                head = head->next;
            }
            //向后遍历处理
            ListNode* curPtr = head;
            while(curPtr->next){
                if(curPtr->next->val == val){
                    curPtr->next = curPtr->next->next;
                }else{
                    curPtr = curPtr->next;
                }
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月15日 下午4:23

    思路:
    首先就是先排除空链表和首节点的情况,
    其次就是遍历整个链表,找出等于val的值并删除。
    时间复杂度,o(n)
    看见复杂度,o(1)
    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode
    deleteNode(ListNode* head, int val) {
    if(headNULL)return head;
    if(head->val
    val)return head->next;
    ListNode*p=head;
    while(p->next&&p->next->val!=val)
    p=p->next;
    p->next=p->next->next;
    return head;
    }

    };

    0 赞同 0 条回复
  • 帆 普通 2023年8月14日 下午11:39

    思路:
    1.头节点是否为空,为空return null
    2.删除的元素为第一个元素,直接删除,返回head.next
    3.删除的元素不是第一个元素
    4.循环查找后续的节点值是否符合条件
    时间复杂度:O(n)
    空间复杂度:O(1)
    Code
    TypeScript

    function deleteNode(head: ListNode | null, val: number): ListNode | null {
        //如果头部节点为空
        if(head) return null;
        //如果删除的是头节点
        if(head.val = val) return head.next;
        //删除的不是头节点
        let current = head.next;//记录下一个节点
        let node = head;   //记录当前节点
        while(current!=null){
            if(current.val= val){
                node.next = current.next;
                return head;
            }
            node = node.next;
            current = node.next;
        }
        return head;
    };
    
    0 赞同 0 条回复
  • 村长คิดถึง 普通 2023年8月14日 下午11:05

    单指针
    func deleteNode(head *ListNode, val int) *ListNode {
    if head.Val val {
    return ead.Next
    }

    pre := head
    for pre.Next != nil && pre.Next.Val != val{
        pre = pre.Next
    }
    
    if pre.Next != nil{
        pre.Next = pre.Next.Next
    }
    
    return head
    

    }

    0 赞同 0 条回复
  • Delete隐身 普通 2023年8月14日 下午10:16

    java代码实现:
    思路:创建一个新节点作为头节点,可以防止头节点是要删除的节点;之后直接遍历即可,找到要删除的节点删除即可
    时间复杂度:O(n),空间复杂度O(1)
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head null) {
    return head;
    }
    ListNode pre = new ListNode(0);
    ListNode cur = head;
    pre.next = cur;
    head = pre;
    while(cur != null) {
    if(cur.val
    val) {
    pre.next = cur.next;
    }
    pre = cur;
    cur = cur.next;
    }

        return head.next;
    }
    

    }

    0 赞同 0 条回复
  • 行动派小彩笔 普通 2023年8月14日 下午10:13
    // 时间复杂读O(n),空间复杂度O(1)
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head == NULL) return NULL;
            if(head->val == val) return head->next;
            ListNode *pre = head;
            ListNode *cur = head->next;
            while(cur!=NULL)
            {
                if(cur->val == val)
                {
                    pre->next = cur->next;
                    return head;
                }
                cur = cur->next;
                pre = pre->next;
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • 💮黧大王.exe 普通 2023年8月14日 下午9:43

    思路
    已知头指针和节点值val,删除节点需要一个指针记录当前节点cur,一个指针记录上一个节点pre,当搜索到当前节点的值与val值相同时就需要先把当前节点指的下一个节点赋给上一个节点指的下一个节点,然后再正常向后移。另外,还需要特殊考虑链表为空和第一个节点就等于val的情况。

    时间复杂度:O(n)
    空间复杂度:O(1)

    Code
    C++

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode
    deleteNode(ListNode* head, int val) {
    if(headNULL) return NULL;
    if(head->val
    val) return head->next;
    ListNode *pre=head;
    ListNode *cur=head->next;
    while(cur!=NULL){
    if(cur->valval){
    pre->next=cur->next;
    }
    pre=cur;
    cur=cur->next;
    }
    return head;
    }
    };

    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年8月14日 下午9:43

    ListNode* deleteNode(ListNode* head, int val) {
    // 创建一个亚结点
    ListNode* dummy = new ListNode(0);
    dummy->next = head;

        ListNode* pre = dummy;
        ListNode* p = head;
    
        while(p->val != val){
            pre = p;
            p = p->next;
        }
        pre->next = p->next;
    
        return dummy->next;
    }
    
    0 赞同 0 条回复
  • 只吃酸菜不吃鱼 普通 2023年8月14日 下午8:20

    Q:如果想在本地想测试这段代码,应该怎么写前面的调用头
    –思路:首先确定双指针法,再特殊处理删除头尾节点情况
    –时间复杂度O(n),空间复杂度O(1)
    /**
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    // 先排除val节点是空节点或者头节点
    if(headnull){
    return null
    }
    if(head.val=val){
    return head.next;
    }
    ListNode tmp=head.next;
    ListNode pre=head;
    while(pre!=null){
    if(tmp.val=val){
    pre.next=tmp.next;
    return head;
    }
    tmp=tmp.next;
    pre.next;
    }
    return head;
    }
    }

    1 赞同 0 条回复
  • 一个人的风海吹了两遍 普通 2023年8月14日 下午5:27

    思路就是逐个遍历,找到了就删掉
    另个意外情况:为空和头结点就是删除节点
    时间复杂度 O(n)
    空间复杂度O(1)
    ps: c++好像需要释放掉删除的节点空间,但java虚拟机会做这件事

       
        public ListNode deleteNode(ListNode head, int val) {
            if(headnull)
                return null;
    
     if(head.val==val)
            return head.next;
    
        ListNode pre = head;
        ListNode temp = head.next;
    
        while(temp!=null){
            if (temp.val==val){
                pre.next=temp.next;
                return  head;
            }
            pre = pre.next;
            temp = temp.next;
        }
        return  head;
    }
    
    0 赞同 0 条回复
  • hhhhh 普通 2023年8月14日 下午5:12

    过

    “`cpp
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode* d = new ListNode(-1);
    d->next = head;
    ListNode *cur;
    cur = d;
    while(cur != nullptr && cur->next != nullptr){
    if(cur->next->val == val){
    cur->next = cur->next->next;
    }
    cur = cur->next;
    }
    return d->next;
    }
    };

    0 赞同 0 条回复
  • 逸兴遄飞 普通 2023年8月14日 上午9:11
    • 思路:遍历,目标节点满足条件则当前的next指向它的下一个
    • 复杂度:时间复杂度O(n),空间复杂度O(1)
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            // 解决链表为空的特殊情况
            if(head == null){
                return null;
            }
            // 对所有值进行依次遍历
            if(head.val == val){
                return head.next;
            }
            // 定义中间链表向其存储当前位置后一位的值
            ListNode temp = head.next;
            // 定义对照链表,便于后续进行剔除操作
            ListNode pre = head;
    
            // 在下一位存在时进行遍历
            while(temp != null){
                // 如果下一位是要剔除的值
                if(temp.val == val){
                    // 直接跳过当前值(剔除操作),将下一位值填入
                    pre.next = temp.next;
                    return head;
                }
                // 依次移位
                temp = temp.next;
                pre = pre.next;
            }
            return head;
        }
    }
    
    1 赞同 0 条回复
  • 晓宇 普通 2023年8月14日 上午7:18
    class Solution(object):
        def deleteNode(self, head, val):
            """
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            """
            if head  None:
                return 
            if head.val  val:
                return head.next
    
      pre = head
        tmp = head.next
    
        while(tmp):
            if tmp.val == val:
                pre.next = tmp.next
                tmp = tmp.next
            else:
                pre = tmp
                tmp = tmp.next
        return  head
    

    链表题目核心则是考虑空节点的next问题,以及前置指针的保留,以及链表的遍历,
    (1)可以先考虑异常情况,解决最简单的case
    (2)可以单删除操作需要哪些元素
    (3)可以考虑正常遍历需要哪些操作
    (4)考虑遇到特殊的值的时候应当如何处理
    把每个问题拆分的写清楚了,就可以解决这个问题

    0 赞同 0 条回复
  • 谁仁 普通 2023年8月14日 上午1:08

    void delNodeByNum(struct nodeList * head,int num)
    {
    //判断是否为空链表
    if(head NULL)
    {
    return;
    }
    struct nodeList *tmp = head;
    //判断是否为删除头结点
    if(num
    1)
    {
    head = tmp->next;
    free(tmp);
    return;
    }
    //找删除节点的前一个节点
    for(int i = 1;tmp != NULL && i < num – 1;i++)
    {
    tmp = tmp->next;
    }
    //看位置是否超过链表总长
    if(tmp NULL || tmp->next NULL)
    {
    return;
    }
    //找到准备删除的下一个节点,删除节点并且将节点续上
    struct nodeList *nextNode = tmp->next->next;
    free(tmp->next);
    tmp->next = nextNode;
    }
    //该删除节点方式是通过检查位数进行删除,遍历链表,时间复杂度o(n),空间复杂度o(1)
    void delNodeByval(struct nodeList * head,int value)
    {
    if(head NULL)
    {
    return;
    }
    struct nodeList *tmp = head;
    struct nodeList *pre = NULL;

    //若删除的是头结点  
    if(tmp != NULL && tmp->data == value)  
    {  
        head = tmp->next;  
        free(tmp);  
        return;  
    }  
    
    //找到要删的节点  
    while(tmp != NULL && tmp->data == value)  
    {  
        pre = tmp;  
        tmp = tmp->next;  
    }  
    
    //若没有匹配,做不删除处理  
    if(tmp == NULL)  
    {  
        return;       
    }  
    //将删除的节点前一个节点跟下一个节点连接上,然后释放删除节点  
    pre->next = tmp->next;  
    free(tmp);  
    

    }
    //通过寻找相同变量的值进行删除节点

    0 赞同 0 条回复
  • 漂洋过海去见你 永久会员 2023年8月13日 下午5:30

    #python解题思路
    class Solution(object):
    def deleteNode(self, head, val):
    “””
    :type head: ListNode
    :type val: int
    :rtype: ListNode
    “””
    if not head:
    return ‘Error’

        if head.val == val:
            return head.next
    
        pre, cur = head, head.next
        while cur and cur.val != val:
            pre, cur = cur, cur.next
    
        if cur:
            pre.next = cur.next
        return head
    
    0 赞同 0 条回复
  • sincere 普通 2023年8月13日 下午4:54
    • 思路:遍历,目标节点满足条件则当前的next指向它的下一个
    • 复杂度:时间复杂度O(n),空间复杂度O(1)
    class Solution {
        public ListNode deleteNode(ListNode head, int target) {
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode pre = dummy;
            while(pre!=null && pre.next != null){
                ListNode post = pre.next;
                if(post.val == target){
                    pre.next = post.next;
                }
                pre = pre.next;
            }
            return dummy.next;
        }
    }
    
    0 赞同 0 条回复
  • QQiing 普通 2023年8月13日 下午4:24

    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if(head NULL){
    return head;
    }
    if(head->val
    val)
    {
    return head->next;
    }
    ListNode *preNode = head;
    ListNode *curNode = head->next;
    while(curNode != nullptr && curNode->val != val)
    {
    preNode = curNode;
    curNode = curNode->next;
    }
    if(curNode != nullptr)
    {
    preNode->next = curNode->next;
    }
    return head;
    }
    };
    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • 大闸谢女士 普通 2023年8月13日 下午4:07

    Definition for singly-linked list.

    class ListNode:

    def init(self, x):

    self.val = x

    self.next = None

    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    dummy = ListNode(0) #初始设置一个伪结点dummy
    dummy.next = head
    # 若头结点就是要删除的点,则直接返回头结点后面的点
    if head.val val:
    return head.next
    while head and head.next:
    #若头结点后面的点是要删除的点,则将要删除点后面一位的值赋给要删除点,这样就将要删除点给删除了
    if head.next.val
    val:
    head.next = head.next.next
    head = head.next
    return dummy.next

    时间复杂度:O(n),空间复杂度:O(1)

    0 赞同 0 条回复
  • 辰北、 普通 2023年8月13日 下午1:36

    //时间复杂度o(n),只要是链表查找时间,先定义一个指针,从头结点开始,他的下一个next不为空的前提下p->next->val和目标值匹配;成功就跳出;然后就替换;输出
    struct ListNode* deleteNode(struct ListNode* head, int val){
    if(head->val val) {
    return head->next;
    }
    struct ListNode *pre=head;

    for(int i=0;((pre->next!=NULL)&& (pre->next->val=!val));i++){
    pre=pre->next;
    }
    if((pre->next!=NULL))
    pre->next=pre->next->next;

    return head;}
    //时间复杂度o(n)链表,前驱指针pre,后驱cur-》next
    struct ListNode* deleteNode(struct ListNode* head, int val){
    if(head->val val) {
    return head->next;
    }
    struct ListNode *pre=head;
    struct ListNode *cur=head;
    while((cur!=NULL)&& (cur->val!=val)){
    pre=cur;
    cur=cur->next
    }
    if(cur!=NULL)
    pre->next=cur->next;
    return head;
    }

    0 赞同 0 条回复
  • 天亮说晚安 普通 2023年8月13日 上午11:21

    思路:首先确定双指针法,再特殊处理删除头尾节点情况
    时间复杂度O(n),空间复杂度O(1)

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(val==head.val){
                head = head.next;
            }
            ListNode p1 = head.next;
            ListNode p2 = head;
            while(p1!=null){
                if(p1.val==val){
                    p2.next = p1.next;
                    p1 = p1.next;
                }
                if(p1==null){
                    break;
                }
                p1 = p1.next;
                p2 = p2.next;
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 天亮之前 普通 2023年8月13日 上午10:51

    /**
    *打卡
    * Definition for singly-linked list.
    * public class ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) { val = x; }
    * }
    */
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head null)
    return null;
    if(head.val
    val){
    return head.next;
    }

        ListNode temp = head.next;
        ListNode pre = head;
    
        while(temp != null){
            if(temp.val == val){
                pre.next = temp.next;
                return head;
            }
            temp = temp.next;
            pre = pre.next;
        }
        return head;
    }
    

    }

    0 赞同 0 条回复
  • Marc 普通 2023年8月13日 上午1:01
    class Solution:
        def deleteNode(self, head: ListNode, val: int) -> ListNode:
            dump = ListNode(-1, head)
            cur = dump 
            while cur.next != None:
                if cur.next.val == val:
                    cur.next = cur.next.next 
                    return dump.next 
                else:
                    cur = cur.next 
            return dump.next
    

    时间 on,空间o1

    0 赞同 0 条回复
  • Jou Siankwa 普通 2023年8月13日 上午12:04
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode cur = dummy, next = cur.next;
            while (next != null){
                if(next.val == val){
                    next = next.next;
                    cur.next = next;
                }else{
                    next = next.next;
                    cur = cur.next;
                }
            }
            return dummy.next;
        }
    }
    

    打卡,基础题

    0 赞同 0 条回复
  • 木子有点甜🍒 普通 2023年8月12日 下午11:39

    “`python
    打卡

    Definition for singly-linked list.

    class ListNode:

    def init(self, x):

    self.val = x

    self.next = None

    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    tmp = ListNode(0)
    tmp.next = head
    node = head
    pre = tmp

        while node.val!=val:
            pre = node
            node = node.next
        pre.next = node.next
        return tmp.next
    
    0 赞同 0 条回复
  • Prime 普通 2023年8月12日 下午11:24

    思路:通过修改指针的指向关系来实现删除效果
    解题方法:
    如果头节点就是要删除的,直接返回头节点的下一个节点 否则遍历链表,找到节点前一个节点 将前一个节点的next指向下下个节点,实现跳过删除效果 最后返回新的头节点
    时间复杂度:O(n)
    空间复杂度:O(1)

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution(object):
        def deleteNode(self, head, val):
            """
            :type head: ListNode
            :type val: int
            :rtype: ListNode
            """
            # 先判断头节点
            if head.val == val:
                return head.next
            cur = head 
            while cur.next:
                if cur.next.val == val:
                    cur.next = cur.next.next
                    return head
                cur = cur.next
            return head
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月12日 下午11:02

    打卡

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head->val == val) return head->next;
            ListNode * pre = head;
            ListNode * cur = head->next;
            while(cur != NULL){
                if(cur->val == val) {
                    pre->next = cur->next;
                    return head;
                }else{
                    pre = cur;
                    cur = cur->next;
                }
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • WA 普通 2023年8月12日 下午10:50
    时间复杂度O(n), 空间复杂度O(1)
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if (!head)  return head;
            if (head -> val == val) return head -> next;
            ListNode* p = head;
            while (p -> next ){
                if (p -> next -> val == val){
                    p -> next = p -> next -> next;
                    return head;
                }
                p = p -> next;
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月12日 下午10:30

    时间复杂度:O(n),空间复杂度:O(1)
    特殊考虑head,用两个节点来连接删除后的链表

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode index = head.next;
            ListNode index1 = head;
            if(head == null){return head.next;}
            if(head.val == val){return head.next;}
    
            while(index.val!=val){
                index1 = index;
                index = index.next;
            }
            index1.next = index.next;
            return head;
        }
    }
    
    0 赞同 0 条回复
  • JustRtn 普通 2023年8月12日 下午10:07

    1.边界第一个节点直接判断,是指定节点直接返回其next;
    2.然后使用指针p遍历链表,last指针表示上一个节点,遍历到直接将last的next指向p的next;
    3.返回head;
    C++
    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode
    deleteNode(ListNode* head, int val) {
    if (head->val val) {
    return head->next;
    }
    auto *last = head;
    for (auto *p = head->next; p != nullptr; p = p->next, last = last->next) {
    if (p->val
    val) {
    last->next = p->next;
    break;
    }
    }
    return head;

    }
    

    };

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月12日 下午9:35

    1.TC : O(n) SC: O(1)
    2.利用prev 儲存刪除節點前一個節點 並用curr去遍歷所有節點
    3.頭節點的corner case要優先判斷

    “`C++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {

    //if the head is null directly return head
    if (head == NULL) {
        return head;
    }
    //if the delete node is the head then return the next node of head
    if (head->val == val) {
        return head->next;
    }
    
    ListNode* curr = head->next;
    ListNode* prev = head;
    
    while (curr != NULL) {
        if (curr->val == val) {
            prev->next = curr->next;
            curr->next = NULL; 
        }
        prev = curr;
        curr = curr->next;
    }  
    return head;
    

    }

    “`

    0 赞同 0 条回复
  • clown 普通 2023年8月12日 下午9:33

    注意while判断条件不是p.next != null
    public ListNode deleteNode(ListNode head, int val) {
    if(head null) return null;
    if(head.val
    val) return head.next;
    ListNode p = head;
    ListNode pre = new ListNode(-1);
    // pre.next = p;
    while(p != null){
    int cur = p.val;
    if(val cur){
    pre.next = p.next;
    p.next = null;
    return head;
    }
    pre = p;
    p = p.next;
    }

        return head;
    }
    
    0 赞同 0 条回复
  • . 永久会员 2023年8月12日 下午9:30

    两个指针向后判断就可以不需要加cur != null,因为cur不可能为null
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    ListNode pre = head,cur=head.next;
    if(head.val val) return head.next;
    while(cur.val !=val){
    pre = cur;
    cur = cur.next;
    }
    pre.next = cur.next;
    return head;
    }
    }

    1 赞同 0 条回复
  • Scanner 永久会员 2023年8月12日 下午9:27

    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * ListNode next;
    * ListNode(int x) : val(x), next(NULL) {}
    * };
    */
    class Solution {
    public:
    ListNode
    deleteNode(ListNode* head, int val) {
    if(head NULL) return NULL; //当头指针为空,返回空指针;

        if(head->val == val) return head->next; //当第一个节点即为需要删除的节点,直接返回下一个节点;
    
        ListNode* temp = head->next; //中间节点;
        ListNode* p = head; //遍历链表;
    
        while(temp != NULL) {
            if(temp->val == val) {
                p->next = temp->next; //让删除达成条件的节点
                return head;
            }
            //向下移走一个节点
            temp = temp->next;
            p = p->next;
        }
    
        return head;
    }
    

    };

    0 赞同 0 条回复
  • 欢⁶ 普通 2023年8月12日 下午9:25

    class Solution(object):
    def deleteNode(self, head, val):
    “””
    :type head: ListNode
    :type val: int
    :rtype: ListNode
    “””
    #处理特殊情况
    if head is None:
    return null
    if head.val val:
    return head.next
    #初始化双指针
    pre = head
    cur = head.next
    #正常处理
    while cur is not None:
    if val
    cur.val:
    pre.next = cur.next
    return head
    pre,cur = pre.next,cur.next
    return head

    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年8月12日 下午9:17

    针对头节点需要进行特殊处理,如果是头节点,直接返回头节点的下一个节点;不是的话,设置前后两个指针,遍历到待删除节点,删除即可

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if (head.val == val) {
                head = head.next;
                return head;
            }
            ListNode p = head;
            ListNode q = p.next;
            while(q != null) {
                if (q.val == val) {
                    p.next = q.next;
                    break;
                }
                p = q;
                q = q.next;
            }
            return head;
        }
    }
    

    时间复杂度: O(n);空间复杂度: O(1)

    0 赞同 0 条回复
  • 小肸 永久会员 2023年8月12日 下午9:02
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
    //针对删除头节点的情况
            ListNode dummyHead = new ListNode(-1);
            dummyHead.next = head;
            ListNode pre = dummyHead;
    //先判空
            while (pre.next != null && pre.next.val != val) {
                pre = pre.next;
            }
            pre.next = pre.next.next;
            return dummyHead.next;
        }
    }
    
    0 赞同 0 条回复
  • Joel 普通 2023年8月12日 下午8:59
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        def deleteNode(self, head: ListNode, val: int) -> ListNode:
            if head.val == val:
                return head.next
            dummy = head
            while True:
                if head.next.val != val:
                    head = head.next
                else:
                    head.next = head.next.next
                    break
            return dummy
    
    
    
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年8月12日 下午8:59

    头结点单独判断
    时间复杂度O(n),空间复杂度O(1)。

    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head->val == val) return head->next;
    
            ListNode* h = new ListNode();
            h->next = head;
            ListNode* e = NULL;
            while(h->next->val != val && h->next->next != NULL) {
                h = h->next;
            }
            if(h->next->val == val && h->next->next != NULL) {
                h->next = h->next->next;
            }
            if(h->next->val == val && h->next->next == NULL) {
                h->next = e;
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • c 普通 2023年8月12日 下午8:50

    双指针
    时间O(n)空间O(1)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if (head.val==val){
                head=head.next;
                return head;
            }
            ListNode pre = head;
            ListNode node = head.next;
            while(node.val!=val){
                pre=node;
                node=node.next;
            }
            pre.next=node.next;
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 别扒拉我 普通 2023年8月12日 下午8:38

    1、基本题
    2、时间复杂度O(n),空间复杂度O(1)

    import java.util.*;
    
    /*
     * public class ListNode {
     *   int val;
     *   ListNode next = null;
     *   public ListNode(int val) {
     *     this.val = val;
     *   }
     * }
     */
    
    public class Solution {
        public ListNode deleteNode (ListNode head, int val) {
            ListNode res = new ListNode(0);
            res.next = head;
            ListNode pre = res;
            ListNode cur = head;
            while(cur != null){
                if(cur.val == val){
                    pre.next = cur.next;
                    break;
                }
                pre = pre.next;
                cur = cur.next;
            }
            return res.next;
        }
    }
    
    0 赞同 0 条回复

# 回答此问题

后才能回答