Skip to content

148. Sort List #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ continually updating 😃.
* [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`*
* [92. Reverse Linked List II](src/0092_reverse_linked_list_2/reverse_linked_list2.go)
* [148. Sort List](src/148_Sort_List/sortlist.go)   *`sort`*
* [203. Remove Linked List Elements](src/0203_remove_linked_list_elements/remove_linked_list_elements.go)
* [206. Reverse Linked List](src/0206_reverse_linked_list/reverse_linked_list.go)
* [237. Delete Node in a Linked List](src/0237_delete_node_in_a_linked_list/dniall.go)
Expand Down
53 changes: 53 additions & 0 deletions src/148_Sort_List/sortlist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
148. Sort List
https://leetcode.com/problems/sort-list/
*/
// time: 2019-03-04

package sortlist

// ListNode Definition for singly-linked list.
type ListNode struct {
Val int
Next *ListNode
}

// merge sort
// time complexity: O(n * log(n))
// using recursion, the system stack is used.
func sortList(head *ListNode) *ListNode {
if head == nil || head.Next == nil {
return head
}

prev, slow, fast := head, head, head
for fast != nil && fast.Next != nil {
prev = slow
slow = slow.Next
fast = fast.Next.Next
}
prev.Next = nil
return merge(sortList(head), sortList(slow))
}

func merge(headA, headB *ListNode) *ListNode {
dummy := &ListNode{}
tail := dummy

for headA != nil && headB != nil {
if headA.Val > headB.Val {
tail.Next = headB
headB = headB.Next
} else {
tail.Next = headA
headA = headA.Next
}
tail = tail.Next
}
if headA != nil {
tail.Next = headA
} else {
tail.Next = headB
}
return dummy.Next
}
24 changes: 24 additions & 0 deletions src/148_Sort_List/sortlist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package sortlist

import (
"reflect"
"testing"
)

func TestSortList(t *testing.T) {
head := createSingleLinkedList([]int{-1, 5, 3, 4, 0})
expected := createSingleLinkedList([]int{-1, 0, 3, 4, 5})
if res := sortList(head); !reflect.DeepEqual(res, expected) {
t.Errorf("expected %v, got %v", expected, res)
}
}

func createSingleLinkedList(nums []int) *ListNode {
dummy := ListNode{}
cur := &dummy
for _, val := range nums {
cur.Next = &ListNode{Val: val}
cur = cur.Next
}
return dummy.Next
}
1 change: 1 addition & 0 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
|0125|[Valid Palindrome](0125_valid_palindrome/valid_palindrome.go)|Easy||
|0136|[136. Single Number](0136_single_number/single_number.go)|Easy|*`hash table;`* *`bit manipulation`*|
|0144|[144. Binary Tree Preorder Traversal](0144_binary_tree_preorder_traversal/binary_tree_preorder_traversal.go)|Medium|*`binary tree`*|
|0148|[148. Sort List](148_Sort_List/sortlist.go)|Medium|*`sort;`* *`linked list`*|
|0150|[150. Evaluate Reverse Polish Notation](0150_evaluate_reverse_polish_notation/evaluate_reverse_polish_notation.go)|Medium|*`stack`*|
|0153|[153. Find Minimum in Rotated Sorted Array](0153_find_minimum_in_rotated_sorted_array/fmirsa.go)|Medium|*`binary search`*|
|0155|[155. Min Stack](0155_min_stack/min_stack.go)|Easy|*`stack`*|
Expand Down