image.png

解题思路

思路一: 迭代 快慢指针

用快指针跳过那些有重复数组,慢指针负责和快指针拼接!

  1. public ListNode deleteDuplicates(ListNode head) {
  2. if (head == null)
  3. return head;
  4. ListNode dummy = new ListNode(-1);
  5. dummy.next = head;
  6. ListNode slow = dummy;
  7. ListNode fast = dummy.next;
  8. while (fast != null){
  9. while (fast.next != null && fast.val == fast.next.val)
  10. fast = fast.next;
  11. if (slow.next == fast)
  12. slow = slow.next;
  13. else
  14. slow.next = fast.next;
  15. fast = fast.next;
  16. }
  17. return dummy.next;
  18. }