NOTE
Remove Duplicates from Sorted List
Given the head head of a sorted linked list, delete duplicate elements so that each value appears only once. Return the sorted linked list.
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
Drag to pan, scroll to zoom, and click a node to expand or collapse it. Large maps start with deeper levels collapsed; click nodes to expand them.
Discussion
Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub