← Home

83. Remove Duplicates from Sorted List

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
83

Let's make this one less mysterious. For 83. Remove Duplicates from Sorted List, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are deleteDuplicates.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Initialize the memory or helper structure.
  2. Process candidates in the order the invariant expects.
  3. Update the answer only when the current state is valid.
  4. Return the value that represents the fully processed input.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n)
  • Space: O(1)

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 12 ms, faster than 100.00% of C++ online submissions for Remove Duplicates from Sorted List.
02//Memory Usage: 9.2 MB, less than 65.42% of C++ online submissions for Remove Duplicates from Sorted List.
03
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 *     int val;
08 *     ListNode *next;
09 *     ListNode(int x) : val(x), next(NULL) {}
10 * };
11 */
12class Solution {
13public:
14    ListNode* deleteDuplicates(ListNode* head) {
15        if(head == NULL || head->next == NULL) return head;
16        
17        ListNode* last = head;
18        ListNode* cur = head->next;
19        
20        while(cur != NULL){
21            if(cur->val == last->val){
22                //remove cur
23                last->next = cur->next;
24            }else{
25                //update last only if cur isn't duplicate
26                last = cur;
27            }
28            cur = cur->next;
29        }
30        
31        return head;
32    }
33};
34
35/**
36Approach 1: Straight-Forward Approach
37**/
38
39/**
40Time: O(n)
41Space: O(1)
42**/
43
44//Runtime: 12 ms, faster than 100.00% of C++ online submissions for Remove Duplicates from Sorted List.
45//Memory Usage: 9.2 MB, less than 55.04% of C++ online submissions for Remove Duplicates from Sorted List.
46
47/**
48class Solution {
49public:
50    ListNode* deleteDuplicates(ListNode* head) {
51        if(head == NULL) return NULL;
52        ListNode* cur = head;
53        while(cur != NULL && cur->next != NULL){
54            if(cur->val == cur->next->val){
55                //remove cur->next
56                cur->next = cur->next->next;
57                //next time we compare cur and cur->next(which was cur->next->next)
58            }else{
59                cur = cur->next;
60            }
61        }
62        return head;
63    }
64};
65**/

Cost

Complexity

Time
O(n)
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(1)
Auxiliary state plus the answer structure where the problem requires one.