Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
Example
Given 1->2->3->4
, and node 3
. return 1->2->4
LeetCode上的原题,请参见我之前的博客Delete Node in a Linked List。
class Solution { public: /** * @param node: a node in the list should be deleted * @return: nothing */ void deleteNode(ListNode *node) { node->val = node->next->val; ListNode *tmp = node->next; node->next = tmp->next; delete tmp; } };
本文转自博客园Grandyang的博客,原文链接:在单链表的中间删除节点[LintCode] Delete Node in the Middle of Singly Linked List ,如需转载请自行联系原博主。