-
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
79 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,22 @@ | ||
package day465 | ||
|
||
// SinglyLL represents a node in a singly linked list. | ||
type SinglyLL struct { | ||
Value interface{} | ||
Next *SinglyLL | ||
} | ||
|
||
// ReverseSinglyLinkedList reverses a singly linked list in O(1) space | ||
// and O(N) time. | ||
func ReverseSinglyLinkedList(head *SinglyLL) *SinglyLL { | ||
var reversed *SinglyLL | ||
|
||
for head != nil { | ||
next := head.Next | ||
head.Next = reversed | ||
reversed = head | ||
head = next | ||
} | ||
|
||
return reversed | ||
} |
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,56 @@ | ||
package day465 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
input []interface{} | ||
}{ | ||
{[]interface{}{"first", 2, "third", 4, "fifth", 6}}, | ||
{[]interface{}{"foo", "bar", "baz"}}, | ||
{[]interface{}{1, 2, 3}}, | ||
} | ||
|
||
func createSinglyLL(data []interface{}) *SinglyLL { | ||
head := &SinglyLL{} | ||
current := head | ||
|
||
for _, value := range data { | ||
current.Next = &SinglyLL{Value: value, Next: nil} | ||
current = current.Next | ||
} | ||
|
||
return head.Next | ||
} | ||
|
||
func wasReversed(head *SinglyLL, data []interface{}) bool { | ||
for i := range data { | ||
if head == nil || head.Value != data[len(data)-1-i] { | ||
return false | ||
} | ||
|
||
head = head.Next | ||
} | ||
|
||
return head == nil | ||
} | ||
|
||
func TestReverseSinglyLinkedList(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, tc := range testcases { | ||
sll := createSinglyLL(tc.input) | ||
if reversed := ReverseSinglyLinkedList(sll); !wasReversed(reversed, tc.input) { | ||
t.Errorf("This list was not reversed correctly: %v", tc.input) | ||
} | ||
} | ||
} | ||
|
||
func BenchmarkReverseSinglyLinkedList(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
sll := createSinglyLL(tc.input) | ||
ReverseSinglyLinkedList(sll) | ||
} | ||
} | ||
} |