正文
[LintCode] Delete Node in the Middle of Singly Linked List 在单链表的中间删除节点
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
Have you met this question in a real interview?
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;
}
};