237. Delete Node in a Linked List 删除链表中的节点
237. Delete Node in a Linked List
思路:用node节点的下一个节点来覆盖node节点,然后删除node的下一个节点
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 void deleteNode(ListNode* node) { 12 ListNode* pNext = node->next; 13 node->val = pNext->val; 14 node->next = pNext->next; 15 16 delete pNext; 17 pNext = nullptr; 18 } 19 };