LeetCode Delete Node in a Linked List (删除链表中的元素)

题意:给一个将要删除的位置的指针,要删除掉该元素。被删元素不会是链尾(不可能删得掉)。

思路:将要找到前面的指针是不可能了,但是可以将后面的元素往前移1位,再删除最后一个元素。

 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         while(node->next->next)//保证node能够在倒数第2个元素上退出
13         {
14             node->val=node->next->val;
15             node=node->next;
16         }
17         node->val=node->next->val;
18         delete(node->next);
19         node->next=0;//注意尾指针清空
20     }
21 };

AC代码

时间: 2024-10-08 11:23:27

LeetCode Delete Node in a Linked List (删除链表中的元素)的相关文章

(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] 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 ->

[CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.EXAMPLEInput: the node c from the linked list a->b->c->d->eResult: nothing is returned, but the new linked list looks like a- >

[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 ->

LeetCode237_Delete Node in a Linked List(删除链表中的节点) Java题解

题目: 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(链表)

Delete Node in a Linked List Total Accepted: 6113 Total Submissions: 12798My Submissions Question Solution 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

lintcode 容易题:Remove Nth Node From End of Lis 删除链表中倒数第n个节点

题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点.  样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null. 注意 链表中的节点个数大于等于n 解题: 要删除倒数第n个节点,我们要找到其前面一个节点,也就是倒数第n+1的节点,找到这个节点就可以进行删除.和上题的思想很类似, 定义两个指针,p和cur,cur指针向前走,走了n

LintCode Python 简单级题目 452.删除链表中的元素

原题描述: 删除链表中等于给定值val的所有节点. 您在真实的面试中是否遇到过这个题? Yes 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5. 标签 链表 题目分析: 删除链表中等于给定值val的所有节点. 遍历链表,找到其中next.val等于val的节点,删除. 注意的地方就是可能链表中的所有元素val都等于val,循环的开始需要从表头开始删除,需要新增一个头节点.

LintCode 删除链表中的元素

删除链表中等于给定值val的所有节点. 样例 给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5 参考@GrandYang  ... 方法1:引入dummy class Solution { public: /** * @param head a ListNode * @param val an integer * @return a ListNode */ ListNode *r

LeetCode——Delete Node in a Linked List

Description: 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