Skip to content

Instantly share code, notes, and snippets.

@EJSohn
Created April 22, 2019 16:30
Show Gist options
  • Select an option

  • Save EJSohn/10e8174a5d15269f02d94d5b9293bc0a to your computer and use it in GitHub Desktop.

Select an option

Save EJSohn/10e8174a5d15269f02d94d5b9293bc0a to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
// check head isn't nil (null)
if head == nil {
return nil
}
// two pointers head, and next node. algorithm is drop next node if it's equal to the first ptr's value.
for ptr1, ptr2 := head, head.Next; ptr2 != nil; ptr2 = ptr2.Next {
// equal
if (ptr1.Val == ptr2.Val) {
// drop next value
ptr1.Next = ptr2.Next
// set second pointer to be traversed to new next node
ptr2 = ptr1
} else {
// traverse ptr1, since it's sorted if the next node is different, there are no more similar nodes.
ptr1 = ptr1.Next
}
}
// pointer to head
return head
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment