image.png

解法1 偷梁换柱

  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. node->val = node->next->val;
  13. node->next = node->next->next;
  14. }
  15. };

解法2 移花接木

  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. *(node) = *(node->next);
  13. }
  14. };