18.2 删除链表中重复的结点

NowCoder

题目描述

18.2 删除链表中重复的结点 - 图1

解题描述

  1. public ListNode deleteDuplication(ListNode pHead) {
  2. if (pHead == null || pHead.next == null)
  3. return pHead;
  4. ListNode next = pHead.next;
  5. if (pHead.val == next.val) {
  6. while (next != null && pHead.val == next.val)
  7. next = next.next;
  8. return deleteDuplication(next);
  9. } else {
  10. pHead.next = deleteDuplication(pHead.next);
  11. return pHead;
  12. }
  13. }

18.2 删除链表中重复的结点 - 图2