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

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

要求:

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

共以下 86 个回答

  • 晚睡晚起起不来 永久会员 2024年3月28日 上午9:36

    打卡1
    首先判断特殊情况
    1.头节点是否为空
    2.要删除的是否为头节点
    然后进行初始化,当前节点和前节点
    之后进行循环,当前节点不为空,进行判断,如果有要删除的值,就删除,没有就接着向下遍历
    最后while循环结束,返回头节点

    java

    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 条回复
  • 熊猫不是猫 普通 2024年3月26日 上午10:20

    “`c++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    //删除头节点
    while (head != nullptr && head->val ==val) {
    ListNode *temp = head;
    head = head->next;
    //delete temp;
    }

        //删除非头节点
        ListNode *cur = head;
        while (cur->next !=nullptr && cur != nullptr) {
            if (cur->next->val == val) {
                ListNode *temp = cur->next;
                cur->next = cur->next->next;
            } else{
                cur = cur->next;
            }
        }
        return head;
    

    }

    };

    “`

    0 赞同 0 条回复
  • RunAsOne 普通 2024年3月11日 下午4:13

    第一次打卡

    # 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
            pre = head
            cur = head.next
            while cur and cur.val != val:
                pre = cur
                cur = cur.next
            if cur: 
                pre.next = cur.next
            return head
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月6日 下午1:44

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

    “`c++
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode* dummynode=new ListNode(0);
    dummynode->next=head;
    ListNode* pn=dummynode;
    while(pn->next!=NULL){
    if(pn->next->val==val){
    pn->next=pn->next->next;
    continue;
    }
    pn=pn->next;
    }
    return dummynode->next;
    }

    0 赞同 0 条回复
  • 🌈 普通 2024年3月5日 下午10:28

    “`C++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode* dummy = new ListNode(0,head);
    ListNode* cur = head;
    ListNode* pre = dummy;
    if(cur == NULL) return head;
    while(cur->val != val){
    cur = cur->next;
    pre = pre->next;
    }
    pre->next = cur->next;
    return dummy->next;
    }
    };
    “`
    头节点

    0 赞同 0 条回复
  • C C 普通 2024年3月5日 上午12:42
    # 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
            pre = head
            cur = head.next
            while cur.val != val and cur:
                pre = cur
                cur = cur.next
            if cur.val == val:  ## 为什么 可以换成 if cur
                pre.next = cur.next
            return head
    
    0 赞同 0 条回复
  • e=mc² 普通 2024年3月4日 下午10:09
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            // 处理head节点为空的情况
            if (head == null) {
                return null;
            }
    
            // 处理删除的节点即为头节点的情况
            if (head.val == val) {
                return head.next;
            }
    
            // 复制一个新的链表 用于遍历移动节点
            ListNode pre = head;
    
            // 当head的下一节点不为空
            while (pre.next != null) {
                // 判断下一节点是否与要删除的val相同
                if (pre.next.val == val) {
                    // head.next = head.next.next
                    // 如果像上面注释的那么写 head会一直变化
                    pre.next = pre.next.next;
                    return head;
                }
                // 节点向后移动
                pre = pre.next;
            }
    
            return head;
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月4日 下午9:59

    python
    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    cur = head
    pre = None

        while cur is not None:
            #找到删除的元素
            if cur.val== val:
                    #元素在头部
                if cur == head:
                    head = cur.next
                else:
                    #元素不在头部
                    pre.next = cur.next
                return head
            else:
                pre = cur
                cur = cur.next
    
    0 赞同 0 条回复
  • June 普通 2024年3月4日 下午8:22

    /**
    * 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 pre = head;//记录上个节点的位置
    ListNode cur = head.next;//要删除的节点
    while (cur!=null){
    if (cur.val val){//说明找到删除的节点了,需要删除
    pre.next = cur.next;
    return head;
    }
    pre = pre.next;
    cur = cur.next;
    }
    return head;
    }
    }

    0 赞同 0 条回复
  • 钱大牙 普通 2024年3月4日 下午6:59

    用两个指针,最后没有查找到的话,直接返回头结点。
    时间O(n)空间O(1)

    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 None:
    return None
    if head.val
    val:
    return head.next
    pre = head
    temp = head.next
    while temp != None:
    if temp.val val:
    pre.next = temp.next
    return head
    else:
    pre = temp
    temp = temp.next
    return head

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月4日 下午4:20

    public ListNode deleteNode(ListNode head, int val) {
    if (head null) return null;
    if (head.val
    val) return head.next;
    ListNode cur = head;
    while (cur.next != null && cur.next.val != val)
    cur = cur.next;
    if (cur.next != null)
    cur.next = cur.next.next;
    return head;
    }
    思路:遍历链表,遇到val值就删除
    时间O(n)空间O(1)

    0 赞同 0 条回复
  • 阿野 普通 2024年3月4日 下午1:54

    “`java
    var deleteNode = function (head, val) {
    //定义头结点和前一个节点
    let cur = pre = new ListNode();
    cur = head.next;//当前节点
    pre = head;
    while (cur != null && cur.val != val) {
    pre = cur;
    cur = cur.next;
    }
    if(cur != null) pre.next = cur.next;
    return head;
    };

    0 赞同 0 条回复
  • Thaddeus 永久会员 2024年3月4日 上午10:40

    打卡

    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(!head) return head;
            if(head->val == val) return head->next;
            ListNode* cur = head->next, *pre = head;
            while(cur) {
                if(cur->val == val) {
                    pre->next = cur->next;
                    break;
                }
                pre = cur;
                cur = cur->next;
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • 小样板 普通 2024年3月4日 上午10:40

    “`java

    思路:因为是单向链表,并且知道头结点和需要删除的节点的值,因此从前向后遍历获取节点的值,与需要删除的值进行比较判断,相等就删除,不相等,就继续遍历下一个节点的值,循环处理。

    /**
    * 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;
    }else{
    head.next = deleteNode(head.next,val);
    }
    return head;
    }
    }

    时间复杂度O(n):删除操作平均需进行N/2次,最多N次
    空间复杂度O(1):head,val只占常数大小空间,没有额外的空间占用。

    0 赞同 0 条回复
  • 我 普通 2024年3月4日 上午1:35

    ”’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 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;
    }
    

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

    0 赞同 0 条回复
  • 山月 普通 2024年3月3日 下午9:24

    class Solution
    {
    // 删除链表元素
    public:
    ListNode* removeElements(ListNode* head, int val)
    {
    ListNode* dummyHead = new ListNode(0); // 实例化dummyHead
    dummyHead->next = head; // 虚拟头结点指向头节点
    ListNode* cur = dummyHead;
    while (cur->next != NULL)
    {
    if(cur->next->val val)
    {
    ListNode* temp = cur->next; //指针temp指向cur->next地址
    cur->next = cur->next->next;//
    delete temp;
    }
    else
    {
    cur = cur->next;
    }

        }
         head = dummyHead->next;
            delete dummyHead;
            return head;
    }
    

    };

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月3日 下午8:54

    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head null) {
    return head;
    }
    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;
    }
    pre = cur;
    cur = cur.next;
    }
    return head;
    }
    }
    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • 东 普通 2024年3月3日 下午8:24

    /**
    * 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) {
    return head.next;
    }
    ListNode pre = head,cur = head.next;
    while(cur != null){
    if(cur.val
    val){
    pre.next = cur.next;
    cur.next = null;
    }
    pre = cur;
    cur = cur.next;
    }
    return head;
    }
    }
    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • =.= 普通 2024年3月3日 下午7:22

    /**
    * 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) {
    ListNode dummy = new ListNode(-1);

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

    }

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

    0 赞同 0 条回复
  • 一颗红豆 普通 2024年3月3日 下午3:52

    时间复杂度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(headnullptr)
    {
    return nullptr;
    }
    if(head->val
    val)
    {
    return head->next;
    }
    ListNode *temp = head->next;
    ListNode *pre = head;
    while(temp!= nullptr)
    {
    if(temp->valval)
    {
    pre->next = temp->next;
    return head;
    }
    temp = temp->next;
    pre = pre->next;
    }
    return head;
    }
    };

    0 赞同 0 条回复
  • 一期一会 普通 2024年3月3日 下午3:10

    用pre指针遍历链表,当遇到pre下一个节点元素为所删除的值时,将pre->next指向pre->next->next,实现删除操作
    对头节点为所删除值得情况,直接返回head->next
    时间复杂度为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 *pre = head;
            if(head->val == val)
            {
                return head->next;
            }
            while(pre->next != NULL && pre->next->val != val)
            {
                pre = pre->next;
            }
            if(pre->next != NULL)
            {
                pre->next = pre->next->next;
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • 小玉 普通 2024年3月3日 下午1:54
    // 正确题解 时间复杂度 O(n) 空间复杂度 O(1)
    // 为什么 p=p->next需要放在else里面
    
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* deleteNode(struct ListNode* head, int val) {
        struct ListNode *p, *q;
        p = (struct ListNode*)malloc(sizeof(struct ListNode));
        q = (struct ListNode*)malloc(sizeof(struct ListNode));
        p->next = head;
        q = p;
        while (p->next != NULL) {
    
            if (p->next->val == val && p->next != NULL) {
    
                struct ListNode* tmp;
                tmp = p->next;
                p->next = p->next->next;
                free(tmp);
    
            } else if (p->next->val == val && p->next == NULL) {
    
                p = NULL;
    
            } else {
    
                p = p->next;
    
            }
        }
    
        return q->next;
    }
    
    0 赞同 0 条回复
  • 雨落星空 普通 2024年3月3日 上午11:17

    思路:
    先判断特殊情况,一种是链表为空,一种为头结点为待删除节点的情况。之后设置双指针,找到待删除节点进行删除。
    时间复杂度:O(N) 空间复杂度:O(1)
    ”’python
    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    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 条回复
  • 一个达不刘 普通 2024年3月3日 上午11:13

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

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

    0 赞同 0 条回复
  • YOLO 普通 2024年3月3日 上午9:33

    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head null)
    return null;

        if(head.val==val){
            head=head.next;
            return head;
        }
        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;
    
    }
    

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

    0 赞同 0 条回复
  • Sam 普通 2024年3月3日 上午12:53

    Definition for singly-linked list.

    class ListNode(object):

    def init(self, x):

    self.val = x

    self.next = None

    class Solution(object):
    def deleteNode(self, node):
    “””
    :type node: ListNode
    :rtype: void Do not return anything, modify node in-place instead.
    “””
    node.val = node.next.val
    node.next = node.next.next

    时间time: O(1)
    空间space: O(1)

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月2日 下午9:48

    java
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head.val val) return head.next;
    ListNode pre = head, cur = head.next;
    while(cur != null && cur.val != val){
    pre = cur;
    cur = cur.next;
    }
    if(cur != null)pre.next = cur.next;
    return head;
    }
    }
    提交

    0 赞同 0 条回复
  • 远方时光 普通 2024年3月2日 下午9:25

    ···python
    class Solution(object):
    def deleteNode(self, head, val):
    “””
    :type head: ListNode
    :type val: int
    :rtype: ListNode
    “””
    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 条回复
  • 小耿 普通 2024年3月2日 下午8:48

    ”’C++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if (head NULL) return head;
    if(head->val
    val) {
    head = head->next;
    return head;
    }
    ListNode* slow = head;
    ListNode* fast = head->next;
    while(fast!= NULL) {
    if(fast->val val) {
    slow->next = fast->next;
    break;
    }
    fast = fast->next;
    slow = slow->next;
    }
    return head;
    }
    };
    思路:

    首先需要特殊判断头节点, 一种是头为空,一种是头节点需要删除
    然后,分别设置快慢指针,相差一个,如果快指针的值val,则需要删除这个节点,让慢指针的next指向快指针的next即可

    0 赞同 0 条回复
  • 晚风、 普通 2024年3月2日 下午8:43

    思路:
    考虑是否是删除头结点
    如果是直接返回head.next
    否则
    用pre和cur分别表示第一个第二个结点,检查cur是否为None 且cur.val是否是我们想要删除的结点值,一直循环,知道cur表示是我们要删除的结点
    然后让pre.next=cur.next此时已经完成删除操作
    返回head就可以
    时间复杂度:o(n)
    空间复杂度:o(1)

    0 赞同 0 条回复
  • Augenstern 普通 2024年3月2日 下午7:57

    代码:

    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 条回复
  • 零 普通 2024年3月2日 下午6:20

    思路:双指针循环遍历,复杂度:O(n)
    代码:

    “`java

    public ListNode deleteNode(ListNode head, int val) {
    if(head==null){
    return head;
    }
    if(val==head.val){
    return head.next;
    }
    ListNode pre=null;
    ListNode cur=head;
    while(null!=cur){
    if(val==cur.val) {
    pre.next=cur.next;
    break;
    }
    pre=cur;
    cur=cur.next;
    }
    return head;
    }

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月2日 下午6:15
    /**
     * 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) {
            ListNode pre = head ,cur = head.next;
            if (pre.val == val) {
                return cur;
            }
            while (cur != null && cur.val != val) {
                pre = cur;
                cur = cur.next;            
            }
            if (cur != null) {
                pre.next = cur.next;
            }
            return head;
        }
    }
    

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

    0 赞同 0 条回复
  • 恭喜发财💰 普通 2024年3月2日 下午5:44

    ”’java
    代码
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head.valval){
    return head.next;
    }
    ListNode pre=new ListNode();
    ListNode p=head;

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

    }

    0 赞同 0 条回复
  • CurDei 普通 2024年3月2日 下午4:58

    遍历列表,如果为空直接返回空,如果头节点为想要删除的节点则直接返回头节点的下一个节点。其他情况就用两个指针遍历,如果后一个指针指向想要删除的节点,则前一个指针直接指向该节点的下一个节点,对其进行删除。
    空间和时间都是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){
    head = head.next;
    return head;
    }
    ListNode pre = head, cur = head.next;
    while(cur != null &&cur.val != val){
    pre = cur;
    cur = cur.next;
    }
    if (cur != null){
    pre.next = cur.next;
    }
    return head;
    }
    }

    0 赞同 0 条回复
  • 哆啦A梦的面包 普通 2024年3月2日 下午4:57
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func deleteNode(head *ListNode, val int) *ListNode {
        if head.Val == val {
            return head.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
    }
    

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

    0 赞同 0 条回复
  • 葡萄吐泡泡 普通 2024年3月2日 下午4:39
    /**
     * 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 pre=head,temp=head.next;
            while(temp!=null){
                if(temp.val==val){
                    pre.next=temp.next;
                    return head;
                }
            pre=pre.next;
            temp=temp.next;
            }
            return head;
        }
    }
    

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

    0 赞同 0 条回复
  • mpweixin用户 普通 2024年3月2日 下午4:33
    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;
        }
    }
    

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

    0 赞同 0 条回复
  • Jankin 普通 2024年3月2日 下午4:19
    /**
     * 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;
            while (temp.next != null) {
                if (temp.next.val == val) {
                    temp.next = temp.next.next;
                    return head;
                }
                temp = temp.next;
            }
            return head;
        }
    }
    

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

    0 赞同 0 条回复
  • Deckkker 普通 2024年3月2日 下午4:14
    # 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 not head:
                return head
    
            if head.val == val:
                return head.next
    
            pre = head
            tmp = head.next
    
            while tmp != None:
                if tmp.val == val:
                    pre.next = tmp.next
                    return head
                pre = pre.next
                tmp = tmp.next
    
    

    设置pre和tmp两个指针:
    pre方便处理删除节点的操作,
    tmp的作用则是寻找目标节点。
    tmp找到目标节点以后,把pre的next指针指向tmp的下一个节点,完成删除操作,并返回头结点。
    如果没有找到则继续移动pre和tmp。

    时间复杂度O(n):最坏情况目标节点在尾节点,要遍历一遍链表。
    空间复杂度O(1):两个指针占用常量空间。

    0 赞同 0 条回复
  • 莫~慢~待 普通 2024年3月2日 下午3:33

    /**
    * 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) {
    ListNode virHead=new ListNode(-1);
    virHead.next=head;
    ListNode node=virHead;
    while(node!=null)
    {
    if((node.next!=null)&&(node.next.valval))
    {
    ListNode temp=node.next;
    node.next=temp.next;
    node=temp.next;
    }
    else
    node=node.next;
    }
    return virHead.next;
    }
    }
    利用虚拟节点方便删除指定值的链表
    时间复杂度 O(N)
    空间复杂度 O(1)

    0 赞同 0 条回复
  • kfaebot 普通 2024年3月2日 下午3:32

    “`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;
    }
    //若头节点指向的不是目标值,利用双指针遍历
    ListNode *slow=head;
    ListNode *fast=head;
    while(fast!=nullptr){
    if(fast->val==val){
    slow->next=fast->next;
    break;
    }
    else{
    slow=fast;
    fast=fast->next;
    }
    }
    return head;
    }
    };
    //时间复杂度O(n)
    //空间复杂度O(1)
    “`

    0 赞同 0 条回复
  • lucky耶耶耶*👻*~ 普通 2024年3月2日 下午3:14
    public class Solution {
        /**
         * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
         *
         *
         * @param head ListNode类
         * @param val int整型
         * @return ListNode类
         */
        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 条回复
  • mpweixin用户 普通 2024年3月2日 下午2:52
    /**
     * 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) {
                return head.next;
            }
            ListNode pre = head, cur = head.next;
            while (cur != null && cur.val != val) {
               pre = cur;
               cur = cur.next;
            }
            if (cur != null) {
                pre.next = cur.next;
                cur.next = null;
            }
            return head;
        }
    }
    

    一般的数据结构教科书上都有图解,注意pre.next = cur.next,这是指让pre节点的下一个直接指向cur节点的下一个,随后把cur和链表断连,余下的cur节点等待gc吃掉就是

    0 赞同 0 条回复
  • NULL 普通 2024年3月2日 下午2:36

    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    // 处理头节点的情况
    if (head != null && head.val val) {
    return head.next;
    }

        ListNode cur = head;
        // 找到要删除节点的前一个节点
        while (cur != null && cur.next != null) {
            if (cur.next.val == val) {
                // 跳过要删除的节点
                cur.next = cur.next.next;
                break; // 找到要删除节点后退出循环
            }
            cur = cur.next;
        }
        return head;
    }
    

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

    0 赞同 0 条回复
  • 墨丘俐 普通 2024年3月2日 下午1:50

    /**
    * 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) {
    ListNode dummy = new ListNode(-1);
    dummy.next = head;
    ListNode pre = dummy;
    ListNode cur = head;
    while(cur!=null){
    if(cur.val!=val){
    pre = cur;
    cur = cur.next;

            }else{
                pre.next = cur.next;
                break;
            }
    
        }
        return dummy.next;
    
    }
    

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

    0 赞同 0 条回复
  • Yuki 永久会员 2024年3月2日 下午1:03

    class Solution {
    public:
    ListNode* removeElements(ListNode* head, int val) {
    if(headnullptr)return nullptr;

        ListNode*dummyHead=new ListNode(0);
        dummyHead->next=head;
        ListNode*pre=dummyHead;
    
        while(pre->next!=nullptr){
            if(pre->next->val==val){
                pre->next=pre->next->next;
            }else{
                pre=pre->next;
            }
        }
        return dummyHead->next;
    }
    

    };
    时间复杂度 O(N)
    空间复杂度 O(1)

    0 赞同 0 条回复
  • 持之以恒:-) 普通 2024年3月2日 下午12:46
    class Solution {
        public ListNode trainningPlan(ListNode l1, ListNode l2) {
            // 两个链表,升序合并
            // 使用双指针 分别指向l1 和 l2
            ListNode node1 = l1;
            ListNode node2 = l2;
            if(node1 == null)
                return node2;
            if(node2 == null)
                return node1;
    
            // 将一个链表作为基础,另一个链表插入其中
            ListNode head = new ListNode();
            // 数据备份
            ListNode insertNode = head;
            // node1 不为空则node1还可以比较
            while(node1 != null){
                // node2 也不为空,且node2 《 node1
                while(node2 != null && node2.val <= node1.val){
                    insertNode.next = node2;
                    insertNode = insertNode.next;
                    node2 = node2.next;
                }
                insertNode.next = node1;
                insertNode = insertNode.next;
                node1 = node1.next;
            }
            // 一方先结束
            if(node1 == null)
                insertNode.next = node2;
            if(node2 == null)
                insertNode.next = node1;
    
            return head.next;
    
        }
    }
    

    时间复杂度O(N + M)空间复杂度)(1)

    0 赞同 0 条回复
  • 望 普通 2024年3月2日 下午12:36

    “`python
    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    pre = ListNode()
    pre.next = head
    head = pre

        while pre.next != None:
            if pre.next.val == val:
                pre.next = pre.next.next
                return head.next
            else:
                pre = pre.next
        return head.next```
    

    虚拟头节点法
    时间:O(N)空间O(1)

    0 赞同 0 条回复
  • 神经蛙 普通 2024年3月2日 上午11:17

    struct ListNode* deleteNode(struct ListNode* head, int val){
    if(NULL head){
    return NULL;
    }

    struct ListNode *curNode = NULL;
    struct ListNode *prevNode = NULL;
    if(head->val == val)
     {
         return head->next;
     }
     curNode = head->next;
     prevNode = head;
     while(curNode)
     {
         if(curNode->val == val)
         {
           prevNode->next = curNode->next;
           break;
         }
         else
         {
             prevNode = curNode;
             curNode = curNode->next;
         }
     }
     return head;
    

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

    0 赞同 0 条回复
  • ChenZhangHangTime 永久会员 2024年3月2日 上午10:29

    我的代码和地哥给的是一样的。我觉得这题难就难在有一些边边的那种你没有考虑到,就比如说假如这个链表是空链表怎么办?假如这个链表第一个就是要删除的元素怎么办?就这两点有点难考虑周全。
    题目的思路是:
    1.先判断这个链表是不是空链表
    2.假如第一个元素就是要删除的元素怎么办
    3.常规的删除链表元素
    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 条回复
  • mpweixin用户 普通 2024年3月2日 上午9:33

    “`java
    /**
    * 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) {
    ListNode res = head;
    if (res.val val)
    return res.next;
    ListNode cur = head;
    ListNode pre = new ListNode(0);
    pre.next = cur;
    while (cur != null) {
    if (cur.val
    val) {
    pre.next = cur.next;
    cur.next = null;
    break;
    } else {
    pre = cur;
    cur = cur.next;
    }
    }
    return res;
    }
    }
    “`java
    时间复杂度O(N),空间复杂度O(1)

    0 赞同 0 条回复
  • HU 普通 2024年3月2日 上午9:32
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} head
     * @param {number} val
     * @return {ListNode}
     */
    var deleteNode = function(head, val) {
        // 设置哑节点
        const dummy = new ListNode(0);
        dummy.next = head;
    
        // 设置当前节点和前驱节点
        let pre = dummy;
        let cur = head;
    
        while(cur != null) {
            if(cur.val == val) {
                pre.next = cur.next;
            }
            cur = cur.next;
            pre = pre.next;
        }
    
        return dummy.next;
    };
    
    0 赞同 0 条回复
  • 😴 普通 2024年3月2日 上午9:16

    /**
    * 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 a = head.next;
        ListNode pre = head;
        while(a != null){
            if(a.val == val){
                pre.next = a.next;
    
            }
            a = a.next;
            pre = pre.next;
        }
        return head;
    

    }
    首先判断该链表是否为空;然后要分为两种情况来讨论,对于头结点,直接判断;非头结点,需要新增一个中间结点,对于值相同的结点进行删除。
    时间复杂度为O(n)
    空间复杂度为O(1)

    0 赞同 0 条回复
  • Free 普通 2024年3月2日 上午8:18
    /**
     * 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* cur = head;
            ListNode* prev = nullptr;
            while(cur)
            {
                if(cur->val == val)
                    prev->next = cur->next;
                prev = cur;
                cur = cur->next;
            }
            return head;
        }
    };
    

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

    0 赞同 0 条回复
  • Yox 普通 2024年3月2日 上午3:56

    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:
    head = head
    cur = head
    next = head.next

        if cur.val == val:
            head = head.next
            return head
    
        while next.val != None:
            if next.val == val:
                next = next.next
                cur.next = next
                return head
    
            cur = cur.next
            next = next.next
    
        return 
    

    straightforward
    time时间:O(n) space空间:O(1)

    0 赞同 0 条回复
  • 统一绿茶Gin 普通 2024年3月1日 下午11:01

    删除链表节点

    /**
     * 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 tmp = head.next;
            ListNode pre = head;
    
            while(tmp != null){
                if(tmp.val == val){
                    pre.next = tmp.next;
                    return head;
                }
                tmp = tmp.next;
                pre = pre.next;
    
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • Bing0 普通 2024年3月1日 下午10:56
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    
    
    struct ListNode* deleteNode(struct ListNode* head, int val){
        if(head == NULL)
            return NULL;
        if(head->val == val)
            return head->next;
        struct ListNode* temp = head->next;
        struct ListNode* pre = head;
        while(temp != NULL){
            if(temp->val == val){
                pre->next = temp->next;
                return head;
            }
            pre = pre->next;
            temp = temp->next;
        }
        return head;
    }
    

    判断异常情况后,逐个遍历去判断是否为删除的节点。
    时间复杂度:O(n);
    空间复杂度:O(1)。

    0 赞同 0 条回复
  • Let it be 永久会员 2024年3月1日 下午10:51

    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head null ){
    return null;
    }
    if(head.val
    val){
    head= head.next;
    }
    ListNode pre = head;
    ListNode now = head.next;
    while(now != null && now.val != val){
    pre = now;
    now = now.next;
    }
    if(now != null)
    pre.next = now.next;
    return head;
    }
    }
    时间复杂 O(N) 空间复杂O(1)

    0 赞同 0 条回复
  • Laboratory 普通 2024年3月1日 下午10:47

    /**
    * 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 pre = new ListNode(-1); // 设置前驱节点
        pre.next = head;
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == val) {
                pre.next = cur.next;
                break;
            } else{
                pre = cur;
                cur = cur.next;
            }
        }
        return head;
    }
    

    }

    先特判,然后应用双指针,保存cur的前节点用作删除
    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • 在线coding 普通 2024年3月1日 下午10:47

    /**
    * 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;
    }
    //虚拟头节点
    ListNode dumyNode = new ListNode(-1);
    dumyNode.next = head;
    //快慢指针
    ListNode fastNode = dumyNode.next;
    ListNode slowNode = dumyNode;
    //当快指针跟要删除的值相等,退出循环,让慢指针直接指向快指针的下一个节点
    while(fastNode.val != val){
    fastNode = fastNode.next;
    slowNode = slowNode.next;
    }
    fastNode = fastNode.next;
    slowNode.next = fastNode;
    return dumyNode.next;
    }
    }

    0 赞同 0 条回复
  • while(1) 普通 2024年3月1日 下午10:46

    “` C++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if(head->val == val) return head->next;
    ListNode *pre = head, *cur = head->next;
    while(cur != nullptr && cur->val != val)
    {
    pre = cur;
    cur = cur->next;
    }
    if(cur != nullptr) pre->next = cur->next;
    return head;
    }
    };
    “`
    方法:双指针
    时间复杂度:o(n)
    空间复杂度:o(1)
    如有问题,欢迎指正!

    0 赞同 0 条回复
  • Marvo 普通 2024年3月1日 下午10:22
    /**
     * 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) {
            // 如果链表为空,直接返回null
            if (head == null){
                return head;
            }
            // 处理头节点需要被删除的情况
            if (head.val == val){
                head = head.next;
                return head;
            }
            // 查找需要删除的节点的前一个节点
            ListNode preNode = head;
            ListNode currentNode = head.next;
            while (currentNode.val != val) {
                preNode = currentNode;
                currentNode = currentNode.next;
            }
            // 如果当前节点为空,说明要删除的节点不存在于链表中
            if (currentNode == null) {
                return head;
            }        
    // 删除找到的节点
            preNode.next = currentNode.next;
            return head;
        }
    
    }
    

    时间复杂度

    此代码段的时间复杂度为 O(n),其中 n 是链表的长度。
    这是因为我们需要遍历链表来查找要删除的节点。
    在最坏的情况下,我们可能需要遍历整个链表才能找到要删除的节点。

    空间复杂度

    此代码段的空间复杂度为 O(1),
    因为我们只使用了有限的额外空间来存储几个指针(preNode 和 currentNode),
    这些空间与链表的长度无关,所以是常数空间复杂度。

    说明

    这段代码的目的是删除链表中的一个特定值的节点。代码首先检查链表是否为空,
    如果为空,则直接返回 null。然后,它检查头节点是否是要删除的节点,如果是,则更新头节点为下一个节点。
    如果头节点不是要删除的节点,代码会遍历链表来查找要删除的节点。
    在遍历过程中,它维护了两个指针:preNode(指向前一个节点)和 currentNode(指向当前节点)。
    当 currentNode 的值等于要删除的值时,代码会删除该节点(通过更新 preNode.next 指向 currentNode.next)。
    如果链表中没有要删除的节点,则函数返回原始的头节点。

    0 赞同 0 条回复
  • !。! 普通 2024年3月1日 下午10:03

    /**
    * 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) {
    while(head != NULL && head->valval){
    head = head->next;
    }
    ListNode* cur=head;
    while(cur->next != NULL && cur != NULL){
    if(cur->next->val
    val){
    cur->next = cur->next->next;
    }
    else{
    cur=cur->next;
    }
    }
    return head;
    }
    };
    时间复杂度: 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* newHead = new ListNode();
    newHead->next = head;
    ListNode* cur = newHead;
    while(cur->next != NULL){
    if(cur->next->val val){
    cur->next = cur->next->next;
    }else{
    cur=cur->next;
    }
    }
    head = newHead->next;
    return head;

    }
    

    };

    0 赞同 0 条回复
  • 紫芝 普通 2024年3月1日 下午9:58

    思路:
    定义一个虚拟头节点,该节点指向给定的头节点,然后依次判断节点值是否等于给定值val,如果相同,则删除该节点:

    “`C++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if (head == NULL) return NULL;
    ListNode *prehead = new ListNode(0);
    prehead->next = head;
    ListNode *cur = prehead;
    while (cur->next) {
    if (cur->next->val == val) {
    cur->next = cur->next->next;
    return prehead->next;
    }
    cur = cur->next;
    }
    return prehead->next;
    }
    };
    “`
    时间复杂度:O(n)
    空间复杂度:O(1)
    关于时空复杂度,如果有错误的,希望大家能帮忙指出来,谢谢!

    0 赞同 0 条回复
  •     普通 2024年3月1日 下午9:58

    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode* p=head;
    while(p!=nullptr)
    {
    if(p->valval) break;
    p=p->next;
    }
    if(p->next
    nullptr)
    {
    ListNode* q=head;
    while(q->next!=p) q=q->next;
    q->next=nullptr;
    }
    else
    {
    p->val=p->next->val;
    p->next=p->next->next;
    }
    return head;
    }
    };
    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • 神经蛙 普通 2024年3月1日 下午9:53

    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 条回复
  • Fancy 普通 2024年3月1日 下午9:50

    打卡
    python
    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    dummy = ListNode(-1)
    dummy.next = head
    pre = dummy
    cur = head
    while cur:
    if cur.val val:
    pre.next = cur.next
    return dummy.next
    pre = pre.next
    cur = cur.next
    return dummy.next

    0 赞同 0 条回复
  • 王位詮釋 普通 2024年3月1日 下午9:46

    /**
    * 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(headnull){
    return head;
    }
    if(head.val
    val){
    return head.next;
    }
    ListNode temp = new ListNode(0);
    temp.next = head;
    while(temp.next!=null){
    if(temp.next.valval){
    temp.next = temp.next.next;
    return head;
    }
    temp = temp.next;
    }
    return null;

    }
    

    }
    时间复杂度:O(n),空间复杂度:O(1)
    思路:遍历链表并对值进行比较,找到需要删除的节点。为了容易处理且放置空指针异常,所以加了一个头节点,每次只需要判断temp(头节点)的next是否为null就可以了,然后一直向下遍历

    0 赞同 0 条回复
  • 刘超波 普通 2024年3月1日 下午9:38

    “`/**
    * 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) {
    ListNode dummy = new ListNode();
    dummy.next = head;
    ListNode p = dummy;

        while (p.next !=null)
        {
            if (p.next.val==val)
            {
                p.next = p.next.next;
            }else{
                p = p.next;
            }
        }
    
        return dummy.next;
    
    }
    

    }
    “`时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • Raphtalia. 普通 2024年3月1日 下午9:35

    /**
    * 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 nullptr) return head;
    if(head->val
    val) return head->next;
    ListNode *tmp = head;
    while(tmp != nullptr){
    if(tmp->next->val val){
    tmp->next = tmp->next->next;
    return head;
    }
    tmp = tmp->next;
    }
    return head;
    }
    };
    时间复杂度O(n),空间复杂度O(1)

    0 赞同 0 条回复
  • 机电皮怪 永久会员 2024年3月1日 下午9:21

    #c++
    ”’
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if ( head nullptr )
    {
    return nullptr;
    }

        //虚拟头节点
        ListNode* noodhead = new ListNode (999, head);
        ListNode* temp = noodhead;
        ListNode* cur = nullptr;
        while ( temp->next->val != val )
        {
            temp = temp->next;
        }
        cur = temp->next->next;
        temp->next = cur;
        //free(cur);
        //free(temp);
        return noodhead->next;
    }
    

    };
    ”’

    0 赞同 0 条回复
  • 时生 普通 2024年3月1日 下午9:17

    //p指针指向删除结点,q指向p的前一个结点,创建头结点,遍历链表
    struct ListNode* deleteNode(struct ListNode* head, int val){
    typedef struct ListNode ListNode;
    ListNode *headNode = malloc(sizeof(ListNode));
    headNode->next = head;
    ListNode *p = head;
    ListNode *q = headNode;

    while(p!=NULL){
    if(p->val != val){
    p = p->next;
    q = q->next;
    }else{
    q->next = p->next;
    p = q->next;
    }
    }
    return headNode->next;
    }

    0 赞同 0 条回复
  • 还不多喝热水 普通 2024年3月1日 下午9:16
    /**
     * 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) {
            // 设置一个虚拟头节点指向链表
            ListNode dummyHead = new ListNode(-1);
            dummyHead.next = head;
    
            // 记录上一个节点
            ListNode prev = dummyHead;
            // 记录当前节点
            ListNode current = prev.next;
            // 如果当前节点不为空
            while(current != null) {
                // 如果当前节点的值等于要删除的节点的值,就把上一个节点的next指针指向当前节点的next节点
                if (current.val == val) {
                    prev.next = current.next;
                    break;
                }
                // 如果当前节点的值等于要删除的节点值,就把当前节点作为上一个节点,当前节点的next节点作为当前节点
                prev = current;
                current = current.next;
            }
            // 返回虚拟头节点的next节点
            return dummyHead.next;
        }
    }
    

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

    0 赞同 0 条回复
  • 白雪公主倒拔垂杨柳 永久会员 2024年3月1日 下午9:14
    /**
     * 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 newhead=head;
            ListNode pre=head;
            ListNode p=head.next;
            if(p==null) return newhead;
    
            while(p.val!=val && p!=null){
                p=p.next;
                pre=pre.next;
            }
            if(p==null) return newhead;
            if(p.val==val){
                pre.next=p.next;
            }
    
            return newhead;
    
        }
    }
    

    思路:
    1. 若头指针为要删除的结点就直接返回,
    2. 从头遍历直至找到要被删除的结点

    时间复杂度o(n) 空间复杂度o(1)

    0 赞同 0 条回复
  • Yeah~ 普通 2024年3月1日 下午9:10

    /**
    * 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 nullptr){
    return nullptr;
    }
    if(head->val
    val){
    return head->next;
    }
    ListNode* tmp = head;
    ListNode* ret;
    for(ListNode* i = tmp->next; i!=nullptr; i = i->next){
    if(i->val val){
    tmp->next = i->next;
    ret = head;
    break;
    }else tmp = i;
    }
    return ret;
    }
    };
    先解决空指针和头节点两个特殊情况,接着for循环遍历,找到后break跳出返回

    0 赞同 0 条回复
  • 云无深雁影 普通 2024年3月1日 下午9:00

    /**
    * 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) {
    ListNode node = head;
    if(node.val val)
    return node.next;
    while(node.next != null){
    if(node.next.val
    val){
    if(node.next.next!=null)
    node.next = node.next.next;
    else
    node.next = null;
    return head;

            }
            node = node.next;
    
        }
    

    return head;
    }
    }
    单指针
    时间复杂度On
    空间复杂度O1

    0 赞同 0 条回复
  • 30tcq 普通 2024年3月1日 下午8:57

    解题思路
    考虑删除的点为头结点和尾结点的特殊情况,使用虚拟头结点。
    犯错
    忘记在所有的删除行为完成后跳出循环,导致超时。
    代码

    /**
     * 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 *newhead = new ListNode();
            newhead->next = head;
            ListNode *pt = newhead;
    
            while(pt->next != NULL){
                if(pt->next->val == val && pt->next->next != NULL){
                    pt->next = pt->next->next;
                    break;
                }else if(pt->next->val == val && pt->next->next == NULL){
                    pt->next = NULL;
                    break;
                }
                pt = pt->next;
            }
            return newhead->next;
        }
    };
    

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

    0 赞同 0 条回复
  • Monou 普通 2024年3月1日 下午8:53

    /**
    * 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) {
    ListNode newHead = new ListNode(0);
    newHead.next = head;
    ListNode node = head;
    ListNode pre = newHead;
    while(node != null ){
    if(node.val val) break;
    pre = node;
    node = node.next;
    }

        if(pre != null){
            if(pre.next != null){
                pre.next = pre.next.next;
            }else{
                pre.next  = null;
            }
        }
        return newHead.next;
    }
    

    }
    复杂度O(n)

    0 赞同 0 条回复
  • 叫不醒装睡的人 普通 2024年3月1日 下午8:46
    class Solution {
        // 已知题目链表中的值各不相同
        // 当首节点为删除节点的特殊情况要考虑到
        // 避免有空节点
        // 前一个节点的后继直接指向后一个节点的后继节点,相当于是删除了当前节点
        public ListNode deleteNode(ListNode head, int val) {
            if (head.next == null) {
                return head;
            }
            if (head.val == val) {
                head = head.next;
                return head;
            }
            ListNode cur = head.next;
            ListNode pre = head;
            while (cur.val != val) {
                pre = cur;
                cur = cur.next;
            }
            pre.next = cur.next;
            return head;
        }
        // 当前代码时间复杂度为 O(n)
        // 因为要通过 while 循环来比较节点中的数值与待删除数值的比较
        // 最好情况是只比较一次,最坏情况是链表中所有元素都比较一次
        // 平均下来时间复杂度为 O(n)
        // 当前代码空间复杂度为 O(1) 
        // 因为只有 cur 和 pre 两个局部变量占用常数大小额外空间
    }
    
    0 赞同 0 条回复
  •  南木 普通 2024年3月1日 下午8:42

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

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */ 
    
    
    class Solution {
    
    <pre><code>public ListNode deleteNode(ListNode head, int val) {
        if (head == null){
            return null;
        }
    
        if(head.val == val){
            head = head.next;
            return head;
        }
    
        ListNode node = head;
        while(node.next != null && node.next.val != val){
            node = node.next;
            if(node == null)
                break;
        }
        node.next = node.next.next;
        return head;
    }
    

    }

    0 赞同 0 条回复
  • Jimmy 普通 2024年3月1日 下午8:42

    删除后就可以返回

    /**
     * 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* cur = head;
            while(cur->next != NULL){
                if(cur->next->val == val){
                    cur->next = (cur->next)->next;
                    return head;
                }
                cur = cur->next;
            }
            return head;
        }
    };
    
    0 赞同 0 条回复
  • 进取 普通 2024年3月1日 下午8:39

    创建头节点来辅助删除结点 LinkList* dummyhead = new LinkList(0,head);

    0 赞同 0 条回复
  • heathhou 普通 2024年3月1日 下午8:38
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            //考虑特殊情况:
            //头指针是否为空指针
            if(head == nullptr){
                return nullptr;
            }
            // 删除头指针
            if(head->val == val){
                return head->next;
            }
    
            // 使用双指针,以便找到待删除的节点时,直接删除节点
            ListNode *pre = head;
            ListNode *temp = head->next;
    
            // 待删除的节点不能为空指针
            while(temp != nullptr){
                // 删除节点
                if(temp->val == val){
                    pre->next = temp->next;
                    return head;
                }
                // 依次遍历
                pre = temp;
                temp = temp->next;
            }
            return head;
        }
    };
    

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

    0 赞同 0 条回复
  • 是hua不是pua 普通 2024年3月1日 下午8:27
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode head1 = new ListNode();
            head1.next = head;
            ListNode pre = head1;
            ListNode cur = head;
            while(cur != null){
                if(cur.val == val){
                    pre.next = cur.next;
                    break;
                }
                pre = cur;
                cur = cur.next;
            }
            return head1.next;
        }
    }
    

    复杂度O(n)

    0 赞同 0 条回复
  • MARS 普通 2024年3月1日 下午8:26

    下面是代码

    function deleteNode(head: ListNode | null, val: number): ListNode | null {
        let node = new ListNode(0, head);
        let cur = node
        while (cur && cur.next) {
            if (cur.next.val <span class="text-highlighted-inline" style="background-color: #fffd38;"> val) {
                cur.next = cur.next.next
            } else {
                cur = cur.next
            }
        }
        return node.next
    };
    
    0 赞同 0 条回复

# 回答此问题

后才能回答