Hyoseo Lee
Home About Projects Blog Thinking
leetcode

83. Remove Duplicates from Sorted List

Topic Linked List
Area Data Structures
Summary
from the beginning, It look at the next node, and it have the same value with current one, it removes the second one. that's all.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Linked List

Intuition

The initial thought to this problem is that it seems easy. and it was.

Approach

from the beginning, It look at the next node, and it have the same value with current one, it removes the second one. that’s all.

Solution

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* deleteDuplicates(struct ListNode* head) {
    if(!head){return NULL;}
    int now = head->val;
    struct ListNode* tmpNode = head;
    while (tmpNode->next){
        if(tmpNode->next->val == now){
            tmpNode->next = tmpNode->next->next;
        }
        else{
            tmpNode = tmpNode->next;
            now = tmpNode->val;
        }
    }
    return head;
}

Complexity

Thoughts

I could complete this chalenge without any note, deepthoughts and anything. I’m genious.