-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package day169 | ||
|
||
// SinglyLL is a singly linked list of integers. | ||
type SinglyLL struct { | ||
Value int | ||
Next *SinglyLL | ||
} | ||
|
||
// MergesortSinglyLinkedList runs in O(N log N) time and O(1) space. | ||
func MergesortSinglyLinkedList(head *SinglyLL) *SinglyLL { | ||
var size int | ||
ptr := head | ||
for ptr != nil { | ||
size++ | ||
ptr = ptr.Next | ||
} | ||
if size == 1 { | ||
return head | ||
} | ||
ptr = head | ||
for i := 0; i < (size/2)-1; i++ { | ||
ptr = ptr.Next | ||
} | ||
half := ptr.Next | ||
ptr.Next = nil | ||
a := MergesortSinglyLinkedList(head) | ||
b := MergesortSinglyLinkedList(half) | ||
return mergeSorted(a, b) | ||
} | ||
|
||
func mergeSorted(a, b *SinglyLL) *SinglyLL { | ||
result := &SinglyLL{} | ||
ptr := result | ||
for a != nil || b != nil { | ||
if a != nil && b != nil { | ||
if a.Value < b.Value { | ||
ptr.Next = a | ||
a = a.Next | ||
ptr = ptr.Next | ||
ptr.Next = nil | ||
} else { | ||
ptr.Next = b | ||
b = b.Next | ||
ptr = ptr.Next | ||
ptr.Next = nil | ||
} | ||
} else if a != nil { | ||
ptr.Next = a | ||
a = a.Next | ||
ptr = ptr.Next | ||
ptr.Next = nil | ||
} else if b != nil { | ||
ptr.Next = b | ||
b = b.Next | ||
ptr = ptr.Next | ||
ptr.Next = nil | ||
} | ||
} | ||
return result.Next | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package day169 | ||
|
||
import "testing" | ||
|
||
var testcases = []struct { | ||
input *SinglyLL | ||
expected *SinglyLL | ||
}{ | ||
{&SinglyLL{4, &SinglyLL{1, &SinglyLL{-3, &SinglyLL{99, nil}}}}, | ||
&SinglyLL{-3, &SinglyLL{1, &SinglyLL{4, &SinglyLL{99, nil}}}}}, | ||
} | ||
|
||
func TestMergesortSinglyLinkedList(t *testing.T) { | ||
t.Parallel() | ||
for tcid, tc := range testcases { | ||
input := copySinglyLL(tc.input) | ||
if result := MergesortSinglyLinkedList(input); !equal(result, tc.expected) { | ||
t.Errorf("Lists don't match for tcid%d", tcid) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkMergesortSinglyLinkedList(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
input := copySinglyLL(tc.input) | ||
MergesortSinglyLinkedList(input) | ||
} | ||
} | ||
} | ||
|
||
func copySinglyLL(a *SinglyLL) *SinglyLL { | ||
result := &SinglyLL{} | ||
ptr := result | ||
for a != nil { | ||
ptr.Next = &SinglyLL{a.Value, nil} | ||
ptr = ptr.Next | ||
a = a.Next | ||
} | ||
return result.Next | ||
} | ||
|
||
func equal(a, b *SinglyLL) bool { | ||
for a != nil && b != nil { | ||
if a.Value != b.Value { | ||
return false | ||
} | ||
a = a.Next | ||
b = b.Next | ||
} | ||
return a == nil && b == nil | ||
} |