LintCode: Delete Node in the Middle of Singly Linked List

时间:2023-03-09 19:16:34
LintCode: Delete Node in the Middle of Singly Linked List

开始没看懂题目的意思,以为是输入一个单链表,删掉链表中间的那个节点。

实际的意思是,传入的参数就是待删节点,所以只要把当前节点指向下一个节点就可以了。

C++

 /**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param node: a node in the list should be deleted
* @return: nothing
*/
void deleteNode(ListNode *node) {
// write your code here
node->val = node->next->val;
node->next = node->next->next;
}
};