【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 -> 4 after calling your function.

题意:

  给定一个单向链表中的任意结点,要求将此结点从链表中删除。

思路:

  比较巧妙,将当前结点伪装成下一个结点,然后将下一个结点删除即可。

C++:

 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
13         if(node == 0)
14             return ;
15
16         node->val  = node->next->val;
17
18         ListNode *del = node->next;
19         node->next = del->next;
20
21         delete del;
22     }
23 };
时间: 2024-10-17 21:17:56

【LeetCode 237】Delete Node in a Linked List的相关文章

【LeetCode OJ 237】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 th

【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 OJ: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——2、Delete Node in a Linked List

这道题的意思呢,是要你写一个函数删除一个节点但是尾节点除外在一个单链表内(英语渣,翻译得别扭...),仅仅给你那个要删除的节点.假如这个链表是 1 -> 2 -> 3 -> 4 ,你被给到的是第三个节点它的值为 3 , 这个链表应该变成这样 1 -> 2 -> 4 在调用了你的函数之后...... 它不能够删除尾节点,因此我们要判断尾节点的情况.要判断一个节点是否为尾节点,只需要判断 node -> next 是否为 空即可.在一般我们写链表的删除操作的时候,都会访问到

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 OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【leetcode 简单】 第七十一题 二叉树的所有路径

给定一个二叉树,返回所有从根节点到叶子节点的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 输入: 1 / 2 3 5 输出: ["1->2->5", "1->3"] 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x