【一天一道LeetCode】#237. Delete Node in a Linked List

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4after calling your function.

(二)解题

题目大意:给定一个单链表中的一个节点,删除它。

解题思路一

删除给定的节点,由于不知道该节点的前驱节点,所以,可以把后面的节点值往前移,然后删除尾部最后一个节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode* pre = node;
        ListNode* p = node->next;
        while(p!=NULL)
        {
            pre->val = p->val;
            if(p->next!=NULL) {//p没有到最后一个节点
                pre = p;
                p = p->next;
            }
            else break;//p->next==NULL,代表遍历到最后一个节点了
        }
        pre->next = NULL;
        delete p;//删除p
    }
};

解题思路二

既然可以改变节点值,那么可以把给定node的后继节点值赋给node,然后删除node的后继节点

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode* temp = node->next;
        node->val = temp->val;
        node->next = temp->next;
        delete temp;//删除node的后继节点
    }
};

很奇怪,思路一的AC时间是12ms,思路二的AC时间是16ms。

想了很久都想不通,大家要是能够解释这个的,可以在下面留言!共同学习!

时间: 2024-08-02 13:39:23

【一天一道LeetCode】#237. Delete Node in a Linked List的相关文章

leetCode 237. Delete Node in a Linked List 链表

237. Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked

leetcode:237 Delete Node in a Linked List-每日编程第四题

Delete Node in a Linked List Total Accepted: 47385 Total Submissions: 107608 Difficulty: Easy Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 ->

LeetCode (237):Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->

[leetcode] 237. Delete Node in a Linked List 解题报告

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->

[LeetCode] 237. Delete Node in a Linked List 解题思路

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->

LeetCode 237 Delete Node in a Linked List(在链表中删除节点)

翻译 给定一个访问节点的路径,写一个函数去删除在一个单向链表中除尾部以外的节点. 假设这个链表是1 -> 2 -> 3 -> 4,并且你被给予了第3个值为3的节点,那么在调用你的函数之后这个链表应该变为1 -> 2 -> 4. 原文 Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the l

(easy)LeetCode 237.Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 ->

leetcode 237 Delete Node in a Linked List python

题目: Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -

Java for LeetCode 237 Delete Node in a Linked List

public class Solution { public void deleteNode(ListNode node) { if(node==null||node.next==null) return; node.val = node.next.val; node.next = node.next.next; } }

237. Delete Node in a Linked List(C++)

237. Delete Node in a Linked Lis t Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked