-
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
95 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,32 @@ | ||
package day398 | ||
|
||
// SinglyLL is a singly linked list of interface{} type. | ||
type SinglyLL struct { | ||
Value interface{} | ||
Next *SinglyLL | ||
} | ||
|
||
// RemoveKthFromEnd removes the k-th node from the | ||
// end of the list and return the head of the list. | ||
// Runs in O(N) time and O(1) space. | ||
func RemoveKthFromEnd(head *SinglyLL, kth int) *SinglyLL { | ||
headPtr := &SinglyLL{Value: nil, Next: head} | ||
front := headPtr | ||
follower := headPtr | ||
|
||
for i := 0; i <= kth; i++ { | ||
if front == nil { | ||
panic("less than k elements in list") | ||
} | ||
|
||
front = front.Next | ||
} | ||
|
||
for ; front != nil; front = front.Next { | ||
follower = follower.Next | ||
} | ||
|
||
follower.Next = follower.Next.Next | ||
|
||
return headPtr.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,62 @@ | ||
package day398 | ||
|
||
import "testing" | ||
|
||
// nolint | ||
var testcases = []struct { | ||
head *SinglyLL | ||
kth int | ||
expected *SinglyLL | ||
}{ | ||
{ | ||
&SinglyLL{1, &SinglyLL{2, &SinglyLL{3, &SinglyLL{4, &SinglyLL{5, nil}}}}}, | ||
2, | ||
&SinglyLL{1, &SinglyLL{2, &SinglyLL{3, &SinglyLL{5, nil}}}}, | ||
}, | ||
{ | ||
&SinglyLL{1, &SinglyLL{2, &SinglyLL{3, &SinglyLL{4, &SinglyLL{5, nil}}}}}, | ||
5, | ||
&SinglyLL{2, &SinglyLL{3, &SinglyLL{4, &SinglyLL{5, nil}}}}, | ||
}, | ||
} | ||
|
||
func TestRemoveKthFromEnd(t *testing.T) { | ||
t.Parallel() | ||
|
||
for tcid, tc := range testcases { | ||
if res := RemoveKthFromEnd(tc.head, tc.kth); !equal(res, tc.expected) { | ||
t.Errorf("TCID%d singly linked lists do not match", tcid) | ||
} | ||
} | ||
} | ||
|
||
func TestRemoveKthFromEndPanic(t *testing.T) { | ||
t.Parallel() | ||
|
||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Errorf("Expected a panic when k is larger than length") | ||
} | ||
}() | ||
|
||
head := &SinglyLL{1, &SinglyLL{2, &SinglyLL{3, &SinglyLL{4, &SinglyLL{5, nil}}}}} | ||
RemoveKthFromEnd(head, 6) | ||
} | ||
|
||
func BenchmarkRemoveKthFromEnd(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for _, tc := range testcases { | ||
RemoveKthFromEnd(tc.head, tc.kth) | ||
} | ||
} | ||
} | ||
|
||
func equal(a, b *SinglyLL) bool { | ||
for ; a != nil && b != nil; a, b = a.Next, b.Next { | ||
if a.Value != b.Value { | ||
return false | ||
} | ||
} | ||
|
||
return a == nil && b == nil | ||
} |