Skip to content

Commit

Permalink
Merge 7aa190a into b1da2cc
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Feb 9, 2019
2 parents b1da2cc + 7aa190a commit 5392adf
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,5 @@ problems from
* [Day 166](https://github.com/vaskoz/dailycodingproblem-go/issues/346)
* [Day 167](https://github.com/vaskoz/dailycodingproblem-go/issues/347)
* [Day 168](https://github.com/vaskoz/dailycodingproblem-go/issues/348)
* [Day 169](https://github.com/vaskoz/dailycodingproblem-go/issues/352)
* [Day 170](https://github.com/vaskoz/dailycodingproblem-go/issues/353)
60 changes: 60 additions & 0 deletions day169/problem.go
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
}
52 changes: 52 additions & 0 deletions day169/problem_test.go
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
}

0 comments on commit 5392adf

Please sign in to comment.