Skip to content

Commit

Permalink
linked-list cleanup (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
spring1843 authored Feb 2, 2025
1 parent be26326 commit 7eef611
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion linkedlist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func addToFront(node *node) {
}
```

A pointer is typically stored for the first and sometimes the last items in a singly linked list. Adding items to the front or back of the list is a constant-time operation. However, deleting the last item can be challenging, as the last item's pointer needs to be updated to the second-to-last item. This is where referencing the last item in each node proves useful. In contrast, doubly linked lists maintain pointers to the previous and next nodes, making deletion operations less expensive.
A pointer is typically stored for the first (head) and sometimes the last (tail) items in a singly linked list. Adding items to the front or back of the list is a constant-time operation. However, deleting the last item can be challenging, as the last item's pointer needs to be updated to the second-to-last item. This is where referencing the last item in each node proves useful. In contrast, doubly linked lists maintain pointers to the previous and next nodes, making deletion operations less expensive.

The Go standard library contains an implementation of [doubly linked lists](https://golang.org/pkg/container/list/). In the following example, numbers from 1 to 10 are added to the list. Even numbers are removed, and the resulting linked list containing odd numbers is printed.

Expand Down
4 changes: 3 additions & 1 deletion linkedlist/reverse_in_place_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ TestReverseLinkedList tests solution(s) with the following signature and problem
func ReverseLinkedList(head *Node) *Node
Reverse a linked list in place. For example given 1->2->3, return 3->2->1.
Reverse a linked list in-place.
For example given 1->2->3, return 3->2->1.
*/
func TestReverseLinkedList(t *testing.T) {
tests := []struct {
Expand Down
13 changes: 12 additions & 1 deletion linkedlist/serialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ TestSerializeAndDeserializeLinkedList tests solution(s) with the following signa
func Serialize(node *Node) string
func Deserialize(stringRepresentation string) *Node
Write a function that turns a linked list into a string representation (Serialize), and then a function
Write a function that turns a linked list of integers into a string representation (Serialize) and a function
that turns that string representation to an actual linked list (Deserialize).
For example consider the following example of a linked list containing numbers 1,2,3:
node1 := &Node{Val: 1}
node2 := &Node{Val: 2}
node3 := &Node{Val: 3}
node1.Next = node2
node2.Next = node3
A string representation of this linked-list should look like 1->2->3. This means that
the node with value 1 is connected to node with value 2, and node with value 2 is connected to node with value 3.
*/
func TestSerializeAndDeserializeLinkedList(t *testing.T) {
tests := []string{
Expand Down

0 comments on commit 7eef611

Please sign in to comment.