Personal Blog
  • 💻Notes for Computer Science
  • Leetcode
    • Array
      • Container with most water
      • 3Sum
      • Next Permutation
      • Valid Sudoku
      • Permutation II
      • Combination Sum
      • Triangle
      • Maximal Square
      • Pairs of Songs with Total Duration Divisible by 60
      • Numbers At Most N Given Digit Set
      • Possible Sum
      • Swap Lex Order
      • Partition Equal Subset Sum
      • Domino and Tromino
      • Numbers At Most N Given Digits
      • Car Pooling
      • Surrounding Regions
      • Min Size Subarray Sum
      • Burst Balloons
      • Jump Game I
      • Jump Game II
      • House Robber II
      • Delete and Earn
      • Word Break
      • Decode Ways
      • Longest Increasing Subsequence
      • Cherry Pickup
      • Rotate Image
    • LinkedList
      • IsListPalindrome
      • Linked List Cycle
      • MergeTwoLinkedList
      • ReverseNodeInKGroup
      • RearrangeLastN
      • Remove Duplicates From Sorted List
      • RemoveKFromList
    • String
      • Generate Parentheses
      • Longest Valid Parentheses
      • Longest Common Subsequence
      • Count and Say
      • Decode String
      • Permutation in String
    • Tree
      • House Robber III
      • Convert Sorted Array to Binary Search Tree
      • Restore Binary Tree
      • Populating Next Right Pointers in Each Node II
      • Subtree of Another Tree
    • Graph
      • All Paths from Source to Target
      • Reorder Routes to Make All Paths Lead to the City Zero
      • Max Points on a Line
  • DBMS
    • DBMS Notes
  • Web App
    • Web Design
    • JavaScript
    • React.js
    • ReactNative
    • Mobile Design
    • Dialogue Flow
  • AnaplanIntern
    • Splunk
    • Docker
    • Kubernetes
  • 💰 Notes for Finance Concept
  • Analysis Concept
    • Volume Spread Analysis
    • Smart Money Concepts
Powered by GitBook
On this page
  • Idea
  • Code
  1. Leetcode
  2. LinkedList

Remove Duplicates From Sorted List

ID: 82

PreviousRearrangeLastNNextRemoveKFromList

Last updated 3 years ago

Given the head of a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Return the linked list sorted as well.

Input: head = [1,2,3,3,4,4,5]
Output: [1,2,5]

Idea

DummyHead + prev track

Make a dummyHead in order to treat duplicates in the middle and duplicates at the head the same. For example, 1,2,3,3,4,4,5 has duplicates in the middle, while 1,1,1,2,3 has duplicates at head. With dummyHead, 1,1,1,2,3 can be treated as duplicates in the middle as well.

Use given head for traversal purpose, and use a reference (prev) to dummyHead for update purpose. If there's duplicates, use while loop to reach the end of this duplicates group, can update the next of prev to the next of head. If no duplicates, advance prev by one step, which is the same as the original head.

Code

public ListNode deleteDuplicates(ListNode head) {
        if(head==null){
            return head;
        } 
        ListNode dummyHead = new ListNode(101, head);
        ListNode prev = dummyHead;
        while(head!=null){
            int base = head.val;
            if(head.next!=null && head.next.val==base){
                while(head.next!=null && head.next.val==base){
                    head = head.next;
                }
                prev.next = head.next;
            }
            else{
               prev = prev.next; 
            }
            head = head.next;
        }
        return dummyHead.next;
    }