NOTE

Remove Duplicates from Sorted List

LeetCode notes on Remove Duplicates from Sorted List.

Data Structures & AlgorithmsCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. Problem Description

Given the head head of a sorted linked list, delete duplicate elements so that each value appears only once. Return the sorted linked list.

2. Approach

3. Implementation

/**
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }
 */
func deleteDuplicates(head *ListNode) *ListNode {
    dummyHead := &ListNode{}
    d := dummyHead
    current := head
    for current != nil {
        next := current.Next
        current.Next = nil

        d.Next = current
        d = d.Next
        for next != nil && next.Val == current.Val {
            next = next.Next
        }

        current = next
    }
    return dummyHead.Next
}

4. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub