372 在O(1)时间复杂度删除链表节点

原题网址:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/

给定一个单链表中的一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。

样例

Linked list is 1->2->3->4, and given node 3, delete the node in place 1->2->4

 

脑子都驴了,认为删除结点一定要找到其前驱节点……(受上题影响?)

当前结点已给出,可以将其后继节点全部值复制到当前结点(要删除的节点)

 1 /**
 2  * Definition of ListNode
 3  * class ListNode {
 4  * public:
 5  *     int val;
 6  *     ListNode *next;
 7  *     ListNode(int val) {
 8  *         this->val = val;
 9  *         this->next = NULL;
10  *     }
11  * }
12  */
13 
14 
15 class Solution {
16 public:
17     /*
18      * @param node: the node in the list should be deletedt
19      * @return: nothing
20      */
21     void deleteNode(ListNode * node) {
22         // write your code here
23         node->val=node->next->val;
24         node->next=node->next->next;
25     }
26 };

参考:

https://blog.csdn.net/xiaowei132/article/details/48139175

https://blog.csdn.net/xiewenjiang110/article/details/46808955

https://www.cnblogs.com/bakari/p/4013812.html

posted @ 2018-03-28 16:07  eeeeeeee鹅  阅读(147)  评论(0编辑  收藏  举报