1
0

82.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //
  2. // Created by 李洋 on 2024/1/15.
  3. //
  4. #ifndef LEECODE_C_82_H
  5. #define LEECODE_C_82_H
  6. // 本题与83题中要求删除重复多余的节点不同,要求只要重复就删除掉所有重复的节点,关机在于链表头的处理
  7. // 这里采用的是创建一个新的头节点,然后对后续节点进行继续处理,一次遍历即可
  8. struct ListNode {
  9. int val;
  10. ListNode *next;
  11. ListNode() : val(0), next(nullptr) {}
  12. ListNode(int x) : val(x), next(nullptr) {}
  13. ListNode(int x, ListNode *next) : val(x), next(next) {}
  14. };
  15. ListNode *deleteDuplicates(ListNode *head) {
  16. if (!head) {
  17. return head;
  18. }
  19. ListNode* dummy = new ListNode(0, head);
  20. ListNode* cur = dummy;
  21. while (cur->next && cur->next->next) {
  22. if (cur->next->val == cur->next->next->val) {
  23. int x = cur->next->val;
  24. while (cur->next && cur->next->val == x) {
  25. cur->next = cur->next->next;
  26. }
  27. }
  28. else {
  29. cur = cur->next;
  30. }
  31. }
  32. return dummy->next;
  33. }
  34. #endif //LEECODE_C_82_H
备用站点 当前处于降级运行的备用站点,仅供应急访问,数据和功能可能不是最新。