diff --git a/README.md b/README.md index 17cd672..ac616d8 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ continually updating 😃. * [24. Swap Nodes in Pairs](src/0024_swap_nodes_in_pairs/swap_nodes_in_pairs.go) * [25. Reverse Nodes in k-Group](./src/0025_reverse_nodes_in_k_group/reverse_node_k_group.go) * [61. Rotate List](./src/0061_rotate_list/rotate_list.go) +* [82. Remove Duplicates from Sorted List II](src/0082_remove_duplicates_from_sorted_list_2/rdfsl.go) ### Dynamic Programming * [62. Unique Paths](./src/0062_unique_paths/unique_paths.go)   *`array`* diff --git a/src/0082_remove_duplicates_from_sorted_list_2/rdfsl.go b/src/0082_remove_duplicates_from_sorted_list_2/rdfsl.go new file mode 100644 index 0000000..850798e --- /dev/null +++ b/src/0082_remove_duplicates_from_sorted_list_2/rdfsl.go @@ -0,0 +1,40 @@ +/* +82. Remove Duplicates from Sorted List II +https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/ + +Given a sorted linked list, delete all nodes that have duplicate numbers, +leaving only distinct numbers from the original list. +*/ +// time: 2019-01-04 + +package rdfsl + +// ListNode Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +// time complexity: O(n) +// space complexity: O(1) +func deleteDuplicates(head *ListNode) *ListNode { + dummyHead := &ListNode{} + dummyHead.Next = head + + pre := dummyHead + cur := head + + for cur != nil && cur.Next != nil { + if cur.Val == cur.Next.Val { + num := cur.Val + for cur != nil && cur.Val == num { + cur = cur.Next + } + pre.Next = cur + } else { + pre = cur + cur = cur.Next + } + } + return dummyHead.Next +} diff --git a/src/0082_remove_duplicates_from_sorted_list_2/rdfsl_test.go b/src/0082_remove_duplicates_from_sorted_list_2/rdfsl_test.go new file mode 100644 index 0000000..78575bc --- /dev/null +++ b/src/0082_remove_duplicates_from_sorted_list_2/rdfsl_test.go @@ -0,0 +1,34 @@ +package rdfsl + +import ( + "reflect" + "testing" +) + +func TestDeleteDuplicates(t *testing.T) { + testCases := []*ListNode{ + createSingleLinkedList([]int{1, 2, 3, 3, 4, 4, 5}), + createSingleLinkedList([]int{1, 1, 1, 2, 3}), + } + + expected := []*ListNode{ + createSingleLinkedList([]int{1, 2, 5}), + createSingleLinkedList([]int{2, 3}), + } + + for index, head := range testCases { + if res := deleteDuplicates(head); !reflect.DeepEqual(res, expected[index]) { + t.Errorf("expected %v, got %v", expected[index], res) + } + } +} + +func createSingleLinkedList(nums []int) *ListNode { + head := &ListNode{} + cur := head + for _, num := range nums { + cur.Next = &ListNode{Val: num} + cur = cur.Next + } + return head.Next +} diff --git a/src/README.md b/src/README.md index 65991c7..738b43c 100644 --- a/src/README.md +++ b/src/README.md @@ -40,6 +40,7 @@ |0077|[77. Combinations](0077_combinations/combinations.go)|Medium|*`backtracking;`**`combine`*| |0079|[79. Word Search](0079_word_search/word_search.go)|Medium|*`backtracking;`**`array`*| |0080|[80. Remove Duplicates from Sorted Array II](0080_remove_duplicates_from_sorted_array2/rdfsa2.go)|Medium|*`double index`*| +|0082|[82. Remove Duplicates from Sorted List II](0082_remove_duplicates_from_sorted_list_2/rdfsl.go)|Medium|*`linked list`*| |0088|[88. Merge Sorted Array](0088_merge_sorted_array/msa.go)|Easy|*`sort`*| |0094|[Binary Tree Inorder Traversal](./0094_binary_tree_inorder_traversal/binary_tree_inorder_traversal.go)|Medium|*`binary tree`*| |0100|[Same Tree](./0100_same_tree/same_tree.go)|Easy|*`binary tree`*|