diff --git a/README.md b/README.md index 4e358f2..919208b 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,7 @@ continually updating 😃. * [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) * [83. Remove Duplicates from Sorted List](src/0083_remove_duplicates_from_sorted_list/rdfsl.go) +* [86. Partition List](src/0086_partition_list/partition_list.go)   *`two pointers`* ### Dynamic Programming * [62. Unique Paths](./src/0062_unique_paths/unique_paths.go)   *`array`* diff --git a/src/0086_partition_list/partition_list.go b/src/0086_partition_list/partition_list.go new file mode 100644 index 0000000..bc96929 --- /dev/null +++ b/src/0086_partition_list/partition_list.go @@ -0,0 +1,45 @@ +/* +86. Partition List +https://leetcode.com/problems/partition-list/ + +Given a linked list and a value x, +partition it such that all nodes less than x come before nodes greater than or equal to x. + +You should preserve the original relative order of the nodes in each of the two partitions. +*/ +// time: 2019-01-04 + +package partitionlist + +// ListNode Definition for singly-linked list. +type ListNode struct { + Val int + Next *ListNode +} + +// time complexity: O(n) +// space complexity: O(1) +func partition(head *ListNode, x int) *ListNode { + var ( + dummyHead1 = &ListNode{} + dummyHead2 = &ListNode{} + cur1 = dummyHead1 + cur2 = dummyHead2 + ) + + for cur := head; cur != nil; { + if cur.Val < x { + cur1.Next = cur + cur1 = cur1.Next + cur = cur.Next + cur1.Next = nil + } else { + cur2.Next = cur + cur2 = cur2.Next + cur = cur.Next + cur2.Next = nil + } + } + cur1.Next = dummyHead2.Next + return dummyHead1.Next +} diff --git a/src/0086_partition_list/partition_list_test.go b/src/0086_partition_list/partition_list_test.go new file mode 100644 index 0000000..95ce0c0 --- /dev/null +++ b/src/0086_partition_list/partition_list_test.go @@ -0,0 +1,27 @@ +package partitionlist + +import ( + "reflect" + "testing" +) + +func createSingleLinkedList(nums []int) *ListNode { + head := &ListNode{} + cur := head + + for _, num := range nums { + cur.Next = &ListNode{Val: num} + cur = cur.Next + } + return head.Next +} + +func TestDeleteDuplicates(t *testing.T) { + testCase := createSingleLinkedList([]int{1, 4, 3, 2, 5, 2}) + + expected := createSingleLinkedList([]int{1, 2, 2, 4, 3, 5}) + x := 3 + if res := partition(testCase, x); !reflect.DeepEqual(res, expected) { + t.Errorf("expected %v, got %v", expected, res) + } +} diff --git a/src/README.md b/src/README.md index a7f19c2..e73ffaf 100644 --- a/src/README.md +++ b/src/README.md @@ -42,6 +42,7 @@ |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`*| |0083|[83. Remove Duplicates from Sorted List](0083_remove_duplicates_from_sorted_list/rdfsl.go)|Easy|*`linked list`*| +|0086|[86. Partition List](0086_partition_list/partition_list.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`*|