Skip to content

Commit 75fbf88

Browse files
authored
feat: Add remove k-th last element (#2)
* feat: Add remove k-th last element from single-linked list. * Fix filenames. * Update docs.
1 parent 408eb0f commit 75fbf88

File tree

9 files changed

+121
-0
lines changed

9 files changed

+121
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ codingcircle contains the examples from the YouTube Coding Cirle.
66

77
So far, the following exercises have been covered:
88

9+
- [Playlist](./playlist/) – removing the k-th last element from a single-linked list
910
- [Trie](./trie/) – implementing an autocomplete feature using a trie
1011

1112
## Running quality assurance

playlist/documentation.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Package playlist provides a single-linked list where the k-th last element
2+
// can be removed.
3+
package playlist

playlist/get_list_values.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package playlist
2+
3+
func GetListValues(head *Node) []int {
4+
node := head
5+
6+
var values []int
7+
for node != nil {
8+
values = append(values, node.Value)
9+
node = node.Next
10+
}
11+
12+
return values
13+
}

playlist/node.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package playlist
2+
3+
type Node struct {
4+
Value int
5+
Next *Node
6+
}
7+
8+
func NewNode(value int, next *Node) *Node {
9+
return &Node{
10+
Value: value,
11+
Next: next,
12+
}
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package playlist
2+
3+
func RemoveKthLastElement(head *Node, k int) *Node {
4+
lead := head
5+
follow := head
6+
7+
for i := 0; i < k; i++ {
8+
if lead == nil {
9+
return nil
10+
}
11+
lead = lead.Next
12+
}
13+
14+
if lead == nil {
15+
return head.Next
16+
}
17+
18+
for lead.Next != nil {
19+
lead = lead.Next
20+
follow = follow.Next
21+
}
22+
23+
follow.Next = follow.Next.Next
24+
25+
return head
26+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package playlist_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
"github.com/thenativeweb/codingcircle/playlist"
8+
)
9+
10+
func TestRemoveKthLastElement(t *testing.T) {
11+
t.Run("remove last element", func(t *testing.T) {
12+
list :=
13+
playlist.NewNode(2,
14+
playlist.NewNode(3,
15+
playlist.NewNode(5,
16+
playlist.NewNode(7,
17+
playlist.NewNode(11, nil)))))
18+
19+
updatedList := playlist.RemoveKthLastElement(list, 1)
20+
values := playlist.GetListValues(updatedList)
21+
22+
assert.Equal(t, []int{2, 3, 5, 7}, values)
23+
})
24+
25+
t.Run("remove first element", func(t *testing.T) {
26+
list :=
27+
playlist.NewNode(2,
28+
playlist.NewNode(3,
29+
playlist.NewNode(5,
30+
playlist.NewNode(7,
31+
playlist.NewNode(11, nil)))))
32+
33+
updatedList := playlist.RemoveKthLastElement(list, 5)
34+
values := playlist.GetListValues(updatedList)
35+
36+
assert.Equal(t, []int{3, 5, 7, 11}, values)
37+
})
38+
39+
t.Run("remove k-th last element", func(t *testing.T) {
40+
list :=
41+
playlist.NewNode(2,
42+
playlist.NewNode(3,
43+
playlist.NewNode(5,
44+
playlist.NewNode(7,
45+
playlist.NewNode(11, nil)))))
46+
47+
updatedList := playlist.RemoveKthLastElement(list, 2)
48+
values := playlist.GetListValues(updatedList)
49+
50+
assert.Equal(t, []int{2, 3, 5, 11}, values)
51+
})
52+
53+
t.Run("with too large k", func(t *testing.T) {
54+
list :=
55+
playlist.NewNode(2,
56+
playlist.NewNode(3,
57+
playlist.NewNode(5,
58+
playlist.NewNode(7,
59+
playlist.NewNode(11, nil)))))
60+
61+
updatedList := playlist.RemoveKthLastElement(list, 17)
62+
63+
assert.Nil(t, updatedList)
64+
})
65+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)