跳到主要内容

237 删除链表中的节点

https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnarn7/

https://leetcode.cn/problems/delete-node-in-a-linked-list/description/

cost: 1m47s

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void deleteNode(ListNode* node) {
node->val = node->next->val;
node->next = node->next->next;
}
};
  • 时间复杂度:O(1)
  • 空间复杂度:O(1)