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

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

要求:
1、请打卡你的解题代码,如果能简单说一下思路则简单说一下思路,请代码不要直接抄袭复制我题解区给的代码哦。
2、给出你代码所用的时间复杂度和空间复杂度

第一次打卡注意:打卡的时候,代码支持 markdown 格式,并且必须要指定语言,比如

共以下 84 个回答

  • hyjob 普通 2023年8月3日 下午3:30

    【删除链表的节点】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) {
            ListNode* dummyHead = new ListNode(-1, head);
            ListNode* p = dummyHead;
            while (p && p->next) {
                if (p->next->val == val) {
                    p->next = p->next->next;
                }
                else {
                    p = p->next;
                }
            }
            return dummyHead->next;
        }
    };
    
    0 赞同 0 条回复
  • 鱼旦 普通 2023年7月26日 上午10:24
    /**
     * Definition for a singly-linked list.
     * class ListNode {
     *     public $val = 0;
     *     public $next = null;
     *     function __construct($val) { $this->val = $val; }
     * }
     */
    class Solution {
    
        /**
         * @param ListNode $head
         * @param Integer $val
         * @return ListNode
         */
        function deleteNode($head, $val) {
            // 创建一个临时节点
            $dummy = new ListNode(0);
            $dummy->next = $head;
            $pre = $dummy;
    
            // 遍历链表
            while ($pre->next != null) {
                if ($pre->next->val == $val) {
                    // 如果当前节点的值等于要删除的节点的值,就将临时节点的next指向当前节点的下一个节点
                    $pre->next = $pre->next->next;
                } else {
                    // 否则,将临时节点的next指向当前节点
                    $pre = $pre->next;
                }
            }
    
            // 返回临时节点的next,即删除后的链表的头节点
            return $dummy->next;
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月24日 下午11: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->val==val)
                return head->next;
            ListNode *pre=head,*cur=head->next;
            while(cur!=NULL){
                if(cur->val==val){
                    pre->next=cur->next;
                    break;
                }
                cur=cur->next;
                pre=pre->next;
            }
            return head;
    
    
    <pre><code>}
    </code></pre>
    
    };
    
    0 赞同 0 条回复
  • 夏唐 普通 2023年7月17日 下午3:40

    打卡

    # 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 == None:
                return None
            if head.val == val:
                return head.next
            temp = head.next
            pre = head
            while temp:
                if temp.val == val:
                    pre.next = temp.next
                    return head
                temp = temp.next
                pre = pre.next
    
            return head
    
    0 赞同 0 条回复
  • 始料未及 普通 2023年7月14日 下午2:55

    、、、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 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;
                return head;
            }
            pre=cur;
            cur=cur.next;
        }
        return head;
    }
    

    }

    0 赞同 0 条回复
  • 吴大水 普通 2023年7月14日 上午6:01

    递归,将等于输入值的节点替换成下一个节点返回,前一个节点指向返回的节点

    “`python
    class Solution:
    def deleteNode(self, head: ListNode, val: int) -> ListNode:
    if head is not None:
    if head.val val:
    head = head.next
    else:
    newhead = self.deleteNode(head.next,val)
    head.next = newhead
    return head
    “`

    0 赞同 0 条回复
  • 🌿 普通 2023年7月12日 下午10:21

    /**
    * 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){
    return head->next;//返回头节点的后续节点
    }
    //******初始化******
    ListNode* cur = head->next;
    ListNode* pre = head;
    //*****删除操作*****
    while (cur != nullptr){
    if (cur->val ){
    pre->next = cur->next;//删除操作
    return head;
    }
    //*****定位节点*****
    cur = cur->next;
    pre = pre->next;
    }
    return head;

    }
    

    };

    时间O(n)
    空间O(1)

    0 赞同 0 条回复
  • 洪小哲 普通 2023年7月12日 下午9:39
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head->next <span class="text-highlighted-inline" style="background-color: #fffd38;"> nullptr)
            {
                return head->val </span> val ? nullptr : head;
            }
            else if(head->val <span class="text-highlighted-inline" style="background-color: #fffd38;"> val)
            {
               return head->next;
            }</span>
    
    <pre><code>    auto fast = head->next;
        auto slow = head;
        while(fast != nullptr)
        {
            if(fast->val == val)
            {
                slow->next = fast->next;
                return head; 
            }
            fast = fast->next;
            slow = slow->next;
        } 
        return head;
    }
    </code></pre>
    
    };
    时间O(n)
    空间O(1)
    
    0 赞同 0 条回复
  • Rona 普通 2023年7月12日 下午6:33

    第一次写的,稍微复杂了一点。注意1:头节点判空 2.最后一个节点是删除节点的情况可以被合并

    # 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: # check none for head node
                return None
            if head.val == val: # check whether head is the special node
                return head.next
    
            cur = head
            prev = None
    
            while cur.next:
                if cur.val != val:
                    prev = cur
                    cur = cur.next
                else:
                    cur = cur.next
                    prev.next = cur
            if cur.val == val:
                prev.next = None
            return head
    

    第二次写的

    # 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: # check none for head node
                return None
            if head.val == val: # check whether head is the special node
                return head.next
            cur = head.next
            prev = head
            while cur: # first node has been checked
                if cur.val != val:
                    prev, cur = cur, cur.next
                else:
                    prev.next = cur.next            
                    return head # found the special node: delete and return
    
    0 赞同 0 条回复
  • 星河 普通 2023年7月12日 下午2:31

    /**
    1.判断头节点是否是要删除的节点
    2.使用快慢指针
    slow := head
    fast := head.Next
    3 若fast nil 则返回head
    4 若fast != nil 则通过fast遍历链表进行比对
    若fast.Val
    val 则 slow.Next = fast.Nest 删除fast节点后跳出循环返回head,(此处fast节点置为nil是否有利于GC回收)
    反之 fast = fast.Next, slow = slow.Next
    5.跳出循环,没有找到要删除的节点,返回head
    */
    func deleteNode(head *ListNode, val int) *ListNode {
    if head nil {
    return head
    }
    if head.Val
    val {
    return head.Next
    }
    slow := head
    fast := head.Next

    for fast != nil  {
        if fast.Val == val {
            slow.Next = fast.Next
            break
        } else {
            fast = fast.Next
            slow = slow.Next
        }
    }
    return head
    

    }

    0 赞同 0 条回复
  • 望仔 普通 2023年7月11日 下午6:56
        /*
            1、先判断第一个节点是不是要删除的,是的话,直接返回 head.Next 即可;
            2、如果第一个节点不是要删除的,那么,循环判断下一个节点是不是要删除的;
        */
       func deleteNode(head *ListNode, val int) *ListNode {
        pre := ListNode{Val: -1, Next: head}
    
        if head == nil || head.Val == val {
            return head.Next
        }
    
        for head.Next != nil {
            if head.Next.Val == val {
                head.Next = head.Next.Next
            }
    
            if head.Next != nil {
                head = head.Next
            }
        }
    
        return pre.Next
    }
    
    0 赞同 0 条回复
  • 余靖 普通 2023年7月11日 下午2:16
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if (nullptr == head)
            {
                return nullptr;
            }
    
            // 引入一个哨兵
            ListNode* sentry = new ListNode(-1);
            sentry->next = head;
    
            ListNode* node = sentry;
            while (nullptr != node->next)
            {
                if (node->next->val == val)
                {
                    break;
                }
                node = node->next;
            }
    
            if (nullptr == node)
            {
                // 没有找到,返回头指针
                return sentry->next;
            }
    
            // 删除节点
            ListNode* node_delete = node->next;
            node->next = node_delete->next;
    
            return sentry->next;
        }
    };
    
    0 赞同 0 条回复
  • momo 普通 2023年7月10日 下午10:06

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

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode p = dummy;
            while (p.next != null) {
                if (val == p.next.val) {
                    p.next = p.next.next;
                    break;
                }
                p = p.next;
            }
            return dummy.next;
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月10日 下午7:34

    ”’c++
    //打卡
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if(head nullptr){
    return nullptr;
    }
    if(head->val
    val){
    return head->next;
    }
    ListNode *dum = new ListNode(head->val);
    dum->next = head;
    head = dum;
    ListNode *p = head;
    ListNode *q = head->next;
    while(q != nullptr){
    if(q->val val){
    p->next = q->next;
    return head->next;
    }
    q = q->next;
    p = p->next;
    }
    return head->next;
    }
    };

    0 赞同 0 条回复
  • 喵喵神教🐈 普通 2023年7月10日 下午7:27
    class Solution:
        def deleteNode(self, head: ListNode, val: int) -> ListNode:
            if head==None:
                return
            if head.val==val:
                return head.next
            p = head.next
            last = head
            while p.val!= val:      #标答 while p!=None: if p.val==val 
                p=p.next
                last=last.next
            else:
                last.next=p.next
                return head
    
    0 赞同 0 条回复
  • NullPointerException 普通 2023年7月10日 下午7:23

    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    // 前驱节点 结果节点
    ListNode pre =new ListNode();
    ListNode result =pre;
    pre.next = head;
    while(head!=null)
    {
    if(head.valval)
    {
    pre.next=head.next;
    head=pre.next;
    return result.next;
    }
    pre=pre.next;
    head=head.next;
    }
    return result.next;
    }

    }

    0 赞同 0 条回复
  • 👾 普通 2023年7月10日 下午3:24

    “`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) {
    //head为空时
    if(head == null) return null;
    //删除的是头节点时
    if(head.val == val) return head.next;
    ListNode pre = head;
    ListNode 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 条回复
  • jaho 普通 2023年7月10日 下午1:55

    //打卡

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head.val <span class="text-highlighted-inline" style="background-color: #fffd38;"> val){
                return head.next;
            }
            ListNode newhead = head;
            while(newhead != null){
                if(newhead.next.val </span> val){
                    newhead.next = newhead.next.next;
                    break;
                }
                newhead = newhead.next;
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 我家旺仔 普通 2023年7月10日 上午11:09

    打卡:

    “`c++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode* current = head;
    ListNode* pre = new ListNode();
    while(current){
    if(val == head->val){
    head = head->next;
    }
    if (current->val == val){
    pre->next = current->next;
    current = nullptr;
    break;
    }else{
    pre = current;
    current = current->next;
    }

        }
        return head;
    
    }
    

    };

    “`

    0 赞同 0 条回复
  • hi 普通 2023年7月10日 上午10:47

    链表节点带*和不带*后续代码书写有区别;
    首先对链表可能出现的特殊情况进行判断,然后循环链表找目标删除

    /**
     * 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* pre = head;
        ListNode* temp = head->next;
    
        while(temp != NULL)
        {
            if(temp->val == val)
            {
                pre->next = temp->next;
                break;
            }
            pre = pre->next;
            temp = temp->next;
        }
        return head;
    }
    }
    
    0 赞同 0 条回复
  • 周小二 普通 2023年7月10日 上午1:39
    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 && cur.val != val) {
                pre = cur;
                cur = cur.next;
            }
            //删除目标节点
            if (cur != null) {
               pre.next = cur.next;
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 神经蛙 普通 2023年7月9日 下午4:36

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

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            // 判断头节点
            if (val == head.val) {
                return head.next;
            }
    
            // 定义变量
            ListNode headNode = head;
            ListNode lastNode = null;
            ListNode node = head;
            // 判断当前节点值是否删除值 && 节点是否遍历结束
            while (val != node.val && node != null) {
                lastNode = node;
                node = node.next;
            }
            // 循环结束,若节点不为null,删除节点
            if (node != null) {
                lastNode.next = node.next;
            }
            // 返回头节点
            return headNode;
        }
    }
    
    
    0 赞同 0 条回复
  • 李敏 普通 2023年7月9日 下午2:54
    # 打卡此问题
    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 != null) {
                    pre.next = cur.next;
                    return head;
                }
                pre = cur;
                cur = cur.next;
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 新禾 普通 2023年7月9日 上午12:57

    维护一个当前指针的前置指针,将前置指针的next指向当前节点的next
    时间复杂度on
    空间复杂度o1
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(head.val val) return head.next;
    ListNode pre = head;
    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年7月8日 下午11:25

    估计也就我用TS写了

    function deleteNode(head: ListNode | null, val: number): ListNode | null {
        let newHead = new ListNode(0,head);
        let cur = newHead;
        while(cur.next) {
            if(cur.next.val <span class="text-highlighted-inline" style="background-color: #fffd38;">= val) {
                cur.next = cur.next.next;
                break;
            }
            cur = cur.next;
        }
        return newHead.next;
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月8日 下午10:41

    代码实现:
    class linkelist:
    def init(self, data):
    self.data = data
    self.next = None

    class Solution:
    def delete(self, head, val):

        if head == None:
            return None
    
        if head.data == val:
            return head.next
    
        temp = head.next
        pre = head
    
        while temp != None:
            if temp.data == val:
                pre.next = temp.next
                return head
            temp = temp.next
            pre = pre.next
    
        return head
    
    0 赞同 0 条回复
  • ㅤ 普通 2023年7月8日 下午10:11
    public ListNode deleteNode(ListNode head, int val) {
            // 先处理特殊场景
            if(head == null) { return null; }
            if(head.val == val) { return head.next; }
            ListNode p = head.next;
            ListNode pre = head;
            // 遍历到当前val == val,或者当前为空两种场景
            while (p != null && p.val != val) {
                pre = p;
                p = p.next;
            }
            // 如果当前不为空,说明存在某个节点有值val,则将当前p去除
            if (p != null) {
                pre.next = p.next;
            }
            return head;
        }
    
    0 赞同 0 条回复
  • CKX 普通 2023年7月8日 下午8:31
    func deleteNode(head *ListNode, val int) *ListNode {
        //删除为头节点的情况
        if head.Val == val {
            head = head.Next
            return head
        }
        //其他情况
        temp := head
        pre := head //保存删除节点的前一个节点
        for temp != nil {
            pre = temp
            temp = temp.Next
            if temp.Val == val {
                temp = pre
                temp.Next = temp.Next.Next
                break
            }
        }
        return head
    }
    

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

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

    代码如下

    function deleteNode(head: ListNode | null, val: number): ListNode | null {
        const dummy = new ListNode(-1, head);
        let pre = dummy;
        while(pre.next){
            if(pre.next.val === val){
                pre.next = pre.next.next
            } else {
                pre = pre.next
            }
        }
        return dummy.next
    };
    
    0 赞同 0 条回复
  • 坏掉的世界 永久会员 2023年7月8日 下午8:00

    代码如下

    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            auto dummy = new ListNode(-1);
            dummy -> next = head;
    
            if(!head){
                return head;
            }
            auto p = dummy;
            while(p && p -> next -> val != val){
                p = p -> next;
            }
            p -> next = p -> next -> next;
            return dummy -> next;
    
        }
    };
    
    0 赞同 0 条回复
  • 土豆芋泥包_ 普通 2023年7月8日 下午7:50

    代码如下

    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 条回复
  • 北荒 普通 2023年7月8日 下午7:35

    时间复杂度:O(n)
    只需要遍历一次整个链表
    空间复杂度:O(1)
    增加了一个临时指针变量

    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head == nullptr){
                return nullptr;
            }
            if(head->val == val){
                return head->next;
            }
            // temp 指向下一个
            // pre 指向头
            ListNode* temp = head->next;
            ListNode* pre = head;
            while (temp != nullptr){
                // temp的值与要删除的值相同,则头指针的next指向temp的next
                if(temp->val == val){
                    pre->next = temp->next;
                    return head;
                }
                // 否则,继续检查后续结点,temp和pre后移
                temp = temp->next;
                pre = pre->next;
            }
            return head;
        }
    };
    
    
    
    
    0 赞同 0 条回复
  • #00456B 普通 2023年7月8日 下午7:21

    写一个dummyHead,next指向head,用于记录head的索引。然后用一个cur指针来进行遍历,后面符合条件的就是用两次next进行跳过,不满足条件的就直接遍历。

    /**
     * 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* dummyHead = new ListNode();
            dummyHead->next = head;
            ListNode* cur = dummyHead;
            while(cur->next != NULL){
                if(cur->next->val == val){
                    cur->next = cur->next->next;
                }
                else{
                    cur = cur->next;
                }
            }
            return dummyHead->next;
        }
    };
    
    0 赞同 0 条回复
  •  普通 2023年7月8日 下午7:05

    下面是C++ 代码

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

    0 赞同 0 条回复
  • ppopp 普通 2023年7月8日 下午5:30

    ˋcpp
    //双指针
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    if(head -> val val) return head -> next; //特例处理,删除第一个节点
    ListNode *pre = head, *p = head -> next; //初始化
    while(p != nullptr && p->val != val){ //定位:当p为空值或者等于val时跳出
    pre = p;
    p = p -> next;
    }
    if(p != nullptr){
    pre -> next = p -> next;
    free(p);
    }//按照p所指节点的情况删除节点
    return head;//返回头指针
    }
    };
    ˋ

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

    “`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) {
    ListNode *newhead = new ListNode(0);
    newhead->next = head;
    ListNode *cur, *next;
    cur = newhead;
    next = newhead->next;
    while (cur != NULL && cur->next != NULL) {
    next=cur->next;
    if (next->val == val) {
    cur->next = next->next;
    // delete next;
    cur = cur->next;
    } else {
    cur = cur->next;
    }
    }
    return newhead->next;
    }
    };
    “`

    0 赞同 0 条回复
  • Ad. 普通 2023年7月8日 下午5:23

    许久没写算法题了,代码可能写复杂了点,但最终也还是通过了
    大概思路就是:
    – 遍历到要删除节点的前一个节点,进行删除操作。链表中的删除操作,无非就是把指针指向进行修改,因此在修改指向前,要把原链表节点记录下来,否则节点会丢失。
    – 另外,需要注意的就是删除的是头节点,则直接将头节点赋给下一个节点即可。
    – 还有链表为空的情况。这种情况就没有可删除的了,直接返回空链表即可。

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            ListNode cur = head;
            if(head == null) return head;
            if(cur.val == val) {
                head = head.next;
                return head;
            }
            while(cur != null) {
                if(cur.next.val == val) {
                    ListNode tmp = cur.next;
                    cur.next = tmp.next;
                    break;
                }
                cur = cur.next;
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 3A87 普通 2023年7月8日 下午5:23
    /**
     * 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;
    
            //找到待删除节点的前一节点 pre
            ListNode* pre=head;
            while((pre->next!=nullptr)&&(pre->next->val!=val))
            {
                pre=pre->next;
            }
    
            //将 pre->next 设置为 pre->next->next。
            if(pre->next!=nullptr)
            {
                pre->next=pre->next->next;
            }
            return head;
        }
    };
    
    /**
     * 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; //当前节点
    
            while((cur!=nullptr&&cur->val!=val)) //遍历节点,直到cur指向要删除的节点
            {
                pre=cur;
                cur=cur->next;
            }
            if(cur!=nullptr)
            {
                pre->next=cur->next;
            }
            return head;
        }
    };
    
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            ListNode* dummy = new ListNode(-1); 
            dummy->next = head; //建立虚拟头节点
            ListNode* cur = dummy;
            while(cur->next) 
                if(cur->next->val == val) cur->next = cur->next->next;
                else cur = cur->next;
            return dummy->next;
        }
    };
    
    0 赞同 0 条回复
  • Rico 普通 2023年7月8日 下午5:18

    时间复杂度O(n)

    class Solution{
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head==nullptr){
                return nullptr;
            }
            if(head->val==val){
                return head->next;
            }
            ListNode* temp = head->next;
            ListNode* pre = head;
    
            while(temp != nullptr){
                if(temp->val==val){
                    pre->next=temp->next;
                    return head;
                }
                temp = temp->next;
                pre = pre->next;
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 小季同学 普通 2023年7月8日 下午4:10

    采用双指针,主要判断头节点为null或只有一个头节点

    function deleteNode(head: ListNode | null, val: number): ListNode | null {
        if(!head) return null
        if(head.val === val) return head.next
        let prev: ListNode = head
        let current: ListNode = head.next
        while(current) {
            if(current.val === val) {
                prev.next = current.next
                break
            } else {
                prev = current
                current = current.next
            }
        }
        return head
    };
    
    0 赞同 0 条回复
  • 裴蕾 普通 2023年7月8日 下午3:43
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head==null) return null;
            if(head.val==val)return head.next;
            ListNode pre=head,cur=head.next;
            while(cur!=null){
                if(cur.val==val){
                    pre.next=cur.next;
                    return head;
                }
                pre=cur;
                cur=cur.next;    
            }
            return head;
        }
    }
    

    不知道为啥markdown格式用不了,就将就一下吧。在此方法中,首先判断链表是否为空,空则输出空。不为空就进入下一步骤。对于不为空的链表,首先判断头节点是否为要删除的节点,若是则直接返回头节点的下一个节点head.next。接着定义当前节点和前置节点,遍历链表,查找要删除的节点,若找到则将前置节点的next指向当前节点的next,即删除节点。最后放回节点即可。本人为纯菜鸟,表达不顺的,请多多谅解。

    0 赞同 0 条回复
  • mpweixin用户 永久会员 2023年7月8日 下午3:24
    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;
                    break;
                }
                temp = temp.next;
            }
            return head;
        }
    }
    
    • 时间复杂度O(n)
    0 赞同 0 条回复
  • 小北 普通 2023年7月8日 下午2:39

    时间复杂度O(n)
    ”’java

    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 条回复
  • 刹那芳间 普通 2023年7月8日 下午2:34
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            //空节点
            if(head == null){
                return null;
            }
            //删除头节点
            if(head.val == val){
                return head.next;
            }
            //删除非头节点
            ListNode cur = head.next;
            ListNode pre = head;
            while(cur != null){
                if(cur.val == val){
                    pre.next = cur.next;
                    break;
                }
                pre = cur;
                cur = cur.next;
            }
    
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 落洛 普通 2023年7月8日 下午2:28

    时间复杂度O(n)

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head == null){
                return null;
            }
    
            if(head.next == null && head.val == val){
                return null;
            }
    
            if(head.next != null && head.val == val){
                return head.next;
            }
    
            ListNode pHead = null;
            ListNode nowHead = head;
            while(nowHead.next != null){
                pHead = nowHead;
                nowHead = nowHead.next;
                if(nowHead.val == val && nowHead.next != null){
                    pHead.next = nowHead.next;
                }
                if(nowHead.val == val && nowHead.next == null){
                    pHead.next = null;
                }
            }   
    
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 一琬人间烟火 普通 2023年7月8日 下午2:20

    时间复杂度:O(n)
    ”’
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode* dummyHead = new ListNode(1);
    dummyHead->next = head;
    ListNode* cur = dummyHead;
    while (cur->next != NULL) {
    if(cur->next->val val) {
    cur->next = cur->next->next;
    break;
    } else {
    cur = cur->next;
    }
    }
    head = dummyHead->next;
    delete dummyHead;
    return head;
    }
    };

    ”’

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月8日 下午1:40
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            // 输入: head = [4,5,1,9], val = 1
            //输出: [4,5,9]
            //解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
            ListNode cur = new ListNode(-1);
            cur.next = head;
            ListNode temp = cur;
            while (temp != null) {
                if (temp.next!=null && temp.next.val == val) {
                    temp.next = temp.next.next;
                }
                temp = temp.next;
            }
            return cur.next;
        }
    }
    
    0 赞同 0 条回复
  • 枫梓。 普通 2023年7月8日 下午12:43
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            ListNode* dummy = new ListNode(0, head);
            ListNode* pre = dummy;
            while(head) {
                if(head->val ==val) {
                    pre->next = head->next;
                    break;
                }
                pre = pre->next;
                head = head->next;
            }
            return dummy->next;
        }
    };
    

    O(n) 双指针删除指定节点

    0 赞同 0 条回复
  • 爱吃胡萝卜🥕的兔子🐰 普通 2023年7月8日 下午12:35
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func getKthFromEnd(head *ListNode, k int) *ListNode {
        fast := head
        slow := head
        for i:=0; i<k; i++{
            fast = fast.Next
        }
    
        for fast != nil{
            fast = fast.Next
            slow = slow.Next
        }
    
        return slow
    }
    

    空间:O1 时间On

    0 赞同 0 条回复
  • 艺 普通 2023年7月8日 下午12:27

    “`c
    /**
    * Definition for singly-linked list.
    * struct ListNode {
    * int val;
    * struct ListNode *next;
    * };
    */
    struct ListNode* deleteNode(struct ListNode* head, int val){
    struct ListNode *p,*q;
    if(head==NULL) return head;
    if(head->val==val) return head->next;
    p=head;
    q=head->next;
    while(q!=NULL)
    {
    if(q->val==val)
    {
    p->next=q->next;
    return head;
    }
    p=p->next;
    q=q->next;
    }
    return head;
    }
    //时间复杂度O(n)
    //空间复杂度O(n)

    0 赞同 0 条回复
  • H、 普通 2023年7月8日 下午12:08

    双指针,O(n)

    /**
     * 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;
            ListNode cur = head.next;
    
            while (cur != null && cur.val != val){
                pre = cur;
                cur = cur.next;
            }
    
            //如果cur==null, 则pre.next已经是null,
            if(cur != null){
                pre.next = cur.next;
            }
    
            return head;
    
        }
    }
    
    0 赞同 0 条回复
  • 富城 普通 2023年7月8日 下午12:03
    /**
     * 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;
            ListNode lat=head.next;
            if(head==null)
            {
                return null;
            }
            else if(head.val==val)
            {
                head=head.next;
                return head;
            }
            else{
                    while(pre!=null)
                    {
                        if(lat.val==val)
                        {
                            pre.next=lat.next;
                            return head;
                        }
                        else{
                            pre=lat;
                            lat=lat.next;
    
                        }
    
                    }
                  return head;   
                }
        }
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月8日 上午11:58

    java
    代码

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

    }

    0 赞同 0 条回复
  • Mooby 普通 2023年7月8日 上午11: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) {
    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 条回复
  • offer收割机 普通 2023年7月8日 上午11:22

    剑指Offer 18.删除链表的节点

    双指针法

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

    时间复杂度:O(n)

    0 赞同 0 条回复
  • 神经蛙 普通 2023年7月8日 上午11:17

    // 打卡
    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    ListNode dummy = new ListNode(-1, head);
    ListNode fast = dummy.next;
    ListNode slow = dummy;
    while(fast != null){
    if(fast.val val){
    slow.next = fast.next;
    break;
    }
    fast = fast.next;
    slow = slow.next;
    }
    return dummy.next;
    }
    }

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月8日 上午10:45

    代码
    public class Node {
    Node next = null;
    int data;
    public Node(int data){
    this.data=data;
    }
    }

    public Node deleteNode(Node head,int val){
    Node pre=head;
    Node cur=pre.next;
    while (cur!=null && cur.data!=val){
    pre=cur;
    cur=cur.next;
    }
    if (cur.dataval)pre.next=cur.next;
    return head;
    }

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

    0 赞同 0 条回复
  • 远行客 普通 2023年7月8日 上午8:46
    class ListNode:
        def __init__(self, x):
            self.val = x
            self.next = None
    
    
    class Solution:
        def deleteNode(self, head, val):
            # 删除的为头节点,直接返回 head.next
            if head.val == val:
                return head.next
    
            # 进行初始化
            pre= head
            # 当前节点
            cur = head.next
    
            # 当cur 为空, 或者 cur 节点值为 val时退出
            while cur and cur.val != val:
                # 保存当前节点
                pre = cur
                # 遍历下一个节点
                cur = cur.next
    
            # cur 指向某个节点, cur 的值等于 val
            if cur:
                # 删除但前节点
                pre.next = cur.next
            # cur 指向 null , 该链表不包含值为 val 的节点
            else:
                pass
    
            # 返回链表头部节点
            return head
    
    
    """
    时间复杂度:O(N), N为链表长度,最差情况需循环N次
    空间复杂度:O(1)
    """
    
    
    
    
    0 赞同 0 条回复
  • 波雅汉库克 普通 2023年7月8日 上午3:39
    class Solution {
    public:
        ListNode* deleteNode(ListNode* head, int val) {
            if(head==nullptr){
                return head;
            }
            ListNode* dummyHead = new ListNode(0);
            dummyHead->next = head;
            ListNode* prev = dummyHead;
            while(head!=nullptr){
                if(head->val == val){
                    prev->next = head->next;
                    break;
                }
                head = head->next;
                prev = prev->next;
            }
            return dummyHead->next;
    };
    

    Time Complexity: O(n) 最糟糕情况删除尾节点走一遍
    Space: O(1)

    0 赞同 0 条回复
  • KCOcean 普通 2023年7月8日 上午12:27

    代码

    ListNode* deleteNode(ListNode* head, int val) {
            ListNode *p,*q;
            if(head == nullptr) return nullptr;
            if(head->val == val) return head->next;
            p = head->next;
            q = head;
            while(p != nullptr){
                if(p->val == val){
                    p = p->next;
                    q->next = p;
                    return head; 
                }
                p = p->next;
                q = q->next;
            }
            return head;
        }
    
    0 赞同 0 条回复
  • w 普通 2023年7月8日 上午12:04
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            //判断链表是否为空,
            if(head == null){
                return null;
            }
            //如果链表第一个值就是要删除的值,头节点则为第二个值
            if(head.val == val){
                return head.next;
            }
            //排除掉特殊情况后,定义双指针:pre表示头节点,temp表示下一个节点
            ListNode pre = head;
            ListNode temp = head.next;
            //当下一个节点不为空时就一直循环
            while(temp!=null){
                if(temp.val == val){
                    //如果下一个节点为指定要删除的节点,则进行删除操作,后返回头节点
                    pre.next = temp.next;
                    return head;
                }else{
                   //否则双指针都往后移一个节点
                    pre = pre.next;
                    temp = temp.next;
                }
            }
            return head;
        }
    }
    
    0 赞同 0 条回复
  • 长河呼叫黄江 普通 2023年7月7日 下午11:52

    下面是代码

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head == null) return null;
            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;
        }
    }
    
    2 赞同 0 条回复
  • 旺旺旺 普通 2023年7月7日 下午11:51

    下面是代码

    # 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==None:
                return None
    
            if head.val==val:
                return head.next
    
            temp=head.next
            pre=head
    
            while temp!=None:
                if temp.val==val:
                    pre.next=temp.next
                    return head
                temp=temp.next
                pre=pre.next
            return head
    
    0 赞同 0 条回复
  • Fighting boy😀 普通 2023年7月7日 下午11:43

    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {

        if (head == nullptr) {
    
            return nullptr;
        }
    
        if (head->val == val) {
    
            return head->next;
        }
    
        ListNode* temp = head->next;
        ListNode* pre = head;
        while(temp != nullptr) {
            if(temp->val == val) {
                pre->next = temp->next;
                return head;
            }
            temp = temp->next;  //更新到下一个节点
            pre = pre->next;
    
        }
        return head;
    }
    

    };

    0 赞同 0 条回复
  • 片言莫语 普通 2023年7月7日 下午11:42
    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 temp = head.next;  //要删除的结点
            while(temp!=null){
                if(temp.val == val){
                     pre.next = temp.next;
                     return head;
                }else{  
                    /*注意:要先移动前结点,不然后移动前结点,temp的值就改变了!
                    pre = temp;  
                    temp = temp.next;  错误! */
                    pre = temp;     //当前结点移动
                    temp = temp.next;   //前结点移动
    
                }
    
            }
           return head;
        }
    }
    
    0 赞同 0 条回复
  • Edenms-u 普通 2023年7月7日 下午11:42
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    
    
    struct ListNode* deleteNode(struct ListNode* head, int val){
        if(head == NULL)
            return head;
        if(head->val == val)
            return head->next;
        struct ListNode* ahead,* bhead;
        ahead = head->next;
        bhead = head;
        while(ahead != NULL){
            if(ahead->val == val){
                if(ahead->next != NULL){
                    bhead->next = ahead->next;
                }else{
                    bhead->next = NULL;
                }
                break;
            }else{
                bhead = ahead;
                ahead = ahead->next;
            }
        }
        return head;
    }
    

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

    0 赞同 0 条回复
  • 是非之欢 普通 2023年7月7日 下午11:38

    “`c++
    class Solution {
    public:
    ListNode* deleteNode(ListNode* head, int val) {
    ListNode *dummy = new ListNode(-1);
    dummy->next = head;

        ListNode *pre = dummy;
        while(pre && pre->next){
            if(pre->next->val  val){
                pre->next = pre->next->next;
            }
            else{
                pre = pre->next;
            }
        }
        return dummy->next;
    }
    

    };

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

    0 赞同 0 条回复
  • 阿尚 普通 2023年7月7日 下午10:54

    class Solution {
    public ListNode deleteNode(ListNode head, int val) {
    if(headnull) return head;
    ListNode dummyNode = new ListNode(0); //建立虚拟节点
    dummyNode.next = head;
    ListNode pre = dummyNode;
    while(head != null && head.val != val){
    pre = pre.next;
    head = head.next;
    }
    pre.next = head.next;
    return dummyNode.next;
    }

    }

    0 赞同 0 条回复
  • momo 普通 2023年7月7日 下午10:36
    struct ListNode {
    
        int val;
    
        struct ListNode *next;
    
    };
    
    
    
    struct ListNode* deleteNode(struct ListNode* head, int val){
    
        // 判断该节点的值是否是头节点
    
        if(head->val == val){
    
            head = head->next;
    
            return head;
    
        }
    
        struct ListNode* temp = head;
    
        struct ListNode* pre = (struct ListNode*)malloc(sizeof(struct ListNode));
    
        pre->next = head;
    
        while(temp->val != val){
    
            temp = temp->next;
    
            pre = pre->next;
    
        }
    
    
    
        // 删除对应的值
    
        if(temp){
    
            pre ->next = temp->next;
    
        }
    
        return head;
    
    }
    
    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月7日 下午10:34

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

    0 赞同 0 条回复
  • @ 李小二 普通 2023年7月7日 下午10:33

    class Solution {
    public ListNode deleteNode(ListNode head, int val) { // 定义了一个方法,用于删除单链表中指定数值的节点
    if(head null){ // 如果头节点为空,直接返回null
    return null;
    }

        if(head.val == val){  // 如果头节点的下一个节点为空,说明只有一个节点,直接返回头节点
            return head.next;
        }
    
        ListNode temp = head.next;  // 创建一个指针temp,指向头节点的下一个节点
        ListNode pre = head;  // 创建一个指针pre,指向头节点
    
        while(temp != null){  // 循环遍历链表
            if(temp.val == val){  // 如果当前节点的值等于指定数值val
                pre.next = temp.next;  // 将前一个节点的指针指向当前节点的下一个节点,即删除了当前节点
                return head;  // 返回头节点
            }
            temp = temp.next;  // 继续遍历到下一个节点
            pre = pre.next;  // 更新pre指针,指向temp的前一个节点
            //这两行代码的作用是继续遍历到下一个节点,并更新pre指针,使其指向temp的前一个节点。
            //temp = temp.next; 将temp指针指向当前节点的下一个节点,即将遍历指针移动到下一个节点。
            //pre = pre.next; 将pre指针指向temp的前一个节点,以便在删除节点时正确地更新链表连接。
            //通过这两行代码,我们实现了在循环中逐个遍历链表元素,并且在找到需要删除的节点后能够正确地更新链表连接关系
        }
    
        return head;  // 返回头节点
    
    
    }
    

    }

    2 赞同 0 条回复
  • 枷锁 普通 2023年7月7日 下午10:33
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head == null) return head;
            ListNode node, preNode;
            if(head.val == val){
                return head.next;
            }
            node = head.next;
            preNode = head;
            while(node != null){
                if(node.val == val){
                    preNode.next = node.next;
                    break;
                }
                preNode = node;
                node = node.next;
            }
            return head;
        }
    }
    

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

    0 赞同 0 条回复
  • mpweixin用户 普通 2023年7月7日 下午10:31

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

    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if(head.val==val) return head.next;
            ListNode tempNode=new ListNode(-1);
            tempNode.next=head;
            while(tempNode.next.val!=val){
                tempNode=tempNode.next;
            }
            tempNode.next=tempNode.next.next;
            return head;
        }
    }
    
    0 赞同 0 条回复
  • June 普通 2023年7月7日 下午10:30
    class Solution {
        public ListNode deleteNode(ListNode head, int val) {
            if (head == null)
                return null;
            if(head.val == val)
                return head.next;
            ListNode fast = head;
            ListNode slow = new ListNode(0);
            slow.next = fast;
            while(fast != null)
            {
                if(fast.val == val){
                    slow.next = fast.next;
                    fast = slow.next;
                }
                else
                {
                    slow = slow.next;
                    fast = fast.next;
                }
            }
            return head;
        }
    }
    

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

    0 赞同 0 条回复
  • Luminous 普通 2023年7月7日 下午10:28
    /**
     * 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 vHead=new ListNode();
            vHead.next=head;
            ListNode p=vHead;
            ListNode cur=head;
            while(cur!=null){
                if(cur.val==val){
                    p.next=cur.next;
                    break;
                }
                p=cur;
                cur=cur.next;
            }
            return vHead.next;
        }
    }
    

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

    1 赞同 0 条回复
  • 雩风廿五 普通 2023年7月7日 下午10:26

    “`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:
    slow = ListNode(-1)
    fast = head
    newhead = slow
    while fast:
    if fast.val != val:
    slow.next = fast
    slow = slow.next
    fast = fast.next
    slow.next = None
    return newhead.next

    0 赞同 0 条回复
  • 朱志刚 普通 2023年7月7日 下午10:24

    执行用时:
    8 ms
    内存消耗:
    9.1 MB

    通过测试用例:
    37 / 37
    /**
    * 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* 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 条回复
  • Terence💧 普通 2023年7月7日 下午10:19
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func deleteNode(head *ListNode, val int) *ListNode {
        if head == nil {return head}
        if head.Val == val {return head.Next}
    
        pre, cur := head, head.Next
        for cur != nil {
            if cur.Val == val {
                pre.Next = cur.Next
                break
            }
            pre = cur
            cur = cur.Next
        }
        return head
    }
    

    试试

    0 赞同 0 条回复
  • finalrgy 普通 2023年7月7日 下午10:16
    1. 下面是代码
    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func deleteNode(head *ListNode, val int) *ListNode {
        virtualHead := &ListNode{Next: head}
        for frontNode, node := virtualHead, virtualHead.Next; node != nil; {
            if node.Val == val {
                frontNode.Next = node.Next
                break
            }
            node = node.Next
            frontNode = frontNode.Next
        }
        return virtualHead.Next
    }
    

    思路:用虚拟头节点减少判断,两个指针指向 前后两个节点 然后按顺序遍历,当后节点的值为目标值,则把前节点指向后节点的下一个节点,最后的链表头节点固定是 虚拟头节点指向的下一个节点

    1. 时间复杂度 O(N) 空间复杂度 O(1)
    0 赞同 0 条回复
  • 醒醒醒醒 普通 2023年7月7日 下午10:14
    // 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* p = head->next;
        ListNode* q = head;
    
        while (p != nullptr){
            if (p->val == val){
                q->next = p->next;
            return head;
            }
            p = p->next;
            q = q->next;
        }
    
        return head;
    
        }
    };
    

    1.思路:先找出头指针为空的情况,当头指针的值为要删除的节点值,返回头结点的下一个指针,定义双指针p,q,p比q快一位,p指向的值为要删除的节点值时,将q的下一位指向p的下一位,返回head。
    2.时间复杂度O(n)
    空间复杂度O(1)

    0 赞同 0 条回复
  • ONE PIECE 普通 2023年7月7日 下午9:17
    /**
     * 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 fast=head.next;
            while(fast!=null){
                if(fast.val==val) pre.next=fast.next;
                pre=pre.next;
                fast=fast.next;
            }
            return head;
        }
    }
    

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

    0 赞同 0 条回复
  • 桎梏 普通 2023年7月7日 下午9:12

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

    public ListNode deleteNode(ListNode head, int val) {
    if(head == null) return null;
    if(head.val == val) return head.next;
    ListNode temp = head.next;
    ListNode cur = head;
    while(temp != null) {
    if(temp.val == val) {
    cur.next = temp.next;
    break;
    }
    cur = cur.next;
    temp = temp.next;
    }
    return head;
    }
    
    0 赞同 0 条回复
  • 北辰 普通 2023年7月7日 下午9:01
    public static ListNode deleteNode(ListNode head, int val) {
            ListNode p = head;
            if(p.val==val) {
                head = p.next;
                return head;
            }
            while(p!=null) {
                if(p.next.val!=val) {
                    p = p.next;
                }else {
                    break;
                }
            }
            p.next = p.next.next;
            return head;
        }
    

    时间复杂度:O(n)
    空间复杂度:O(1)
    思路:保持head结点不动,重新定义一个结点初始指向头结点。因为要删除指定值的节点所以我们需要该结点指向要删除节点的上一个结点,所以判断该结点的下一个结点的val值是不是要删除的节点。是就跳出循环,不是就将结点指向下一个节点位置。如果找到了将该结点指向下下一个节点。
    需要特判的是head结点的val值是不是要删除的结点,此时头结点需要更新。

    0 赞同 0 条回复
  • 云雀 普通 2023年7月7日 下午8:59

    它说评论必须包含中文。

    class Solution:
        def deleteNode(self, head: ListNode, val: int) -> ListNode:
            if head.val == val:
                if not head.next:
                    return None
                else:
                    return head.next
            pre = head
            cur = head.next
            while cur:
                if cur.val == val:
                    pre.next = cur.next
                    break
                cur = cur.next
                pre = pre.next
            return head
    
    0 赞同 0 条回复

# 回答此问题

后才能回答