我也来刷LeetCode——2、Delete Node in a Linked List

  这道题的意思呢,是要你写一个函数删除一个节点但是尾节点除外在一个单链表内(英语渣,翻译得别扭...),仅仅给你那个要删除的节点。假如这个链表是 1 -> 2 -> 3 -> 4 ,你被给到的是第三个节点它的值为 3 , 这个链表应该变成这样 1 -> 2 -> 4 在调用了你的函数之后......

  它不能够删除尾节点,因此我们要判断尾节点的情况。要判断一个节点是否为尾节点,只需要判断 node -> next 是否为 空即可。在一般我们写链表的删除操作的时候,都会访问到要删除节点的上一个节点,然后将它的上一个节点的 next 指向要删除节点的下一个节点,再释放该节点。

  而在这里,我们无法访问要删除节点的上一个节点,所以不能通过最简单的改变指针的指向来进行删除操作。但是我们可以通过复制节点的方式,来达到最终的效果。我们将要删除节点的下一个节点的值复制到要删除的节点上,此时两个节点完全一样,我们再释放一个节点,这样就等同于删除了一个节点。所以针对这道题目来说答案就是这样,虽然改变指针指向是最简单的方式......

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     void deleteNode(ListNode* node) {
12         if (node == nullptr) {
13             return;
14         }
15         if (node->next != nullptr){
16             ListNode* t = node->next;
17             node->val = node->next->val;
18             node->next = node->next->next;
19             delete t;
20         }
21     }
22 };

  

时间: 2024-10-17 21:18:01

我也来刷LeetCode——2、Delete Node in a Linked List的相关文章

[LeetCode][JavaScript]Delete Node in a Linked List

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

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】Delete Node in a Linked List

原题链接:https://leetcode.com/problems/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

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: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]79. 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 ->

Java [Leetcode 273]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