Created
April 22, 2019 16:30
-
-
Save EJSohn/10e8174a5d15269f02d94d5b9293bc0a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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