Skip to content

Commit 7eef611

Browse files
authored
linked-list cleanup (#171)
1 parent be26326 commit 7eef611

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

linkedlist/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func addToFront(node *node) {
4444
}
4545
```
4646

47-
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.
47+
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.
4848

4949
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.
5050

linkedlist/reverse_in_place_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ TestReverseLinkedList tests solution(s) with the following signature and problem
1212
1313
func ReverseLinkedList(head *Node) *Node
1414
15-
Reverse a linked list in place. For example given 1->2->3, return 3->2->1.
15+
Reverse a linked list in-place.
16+
17+
For example given 1->2->3, return 3->2->1.
1618
*/
1719
func TestReverseLinkedList(t *testing.T) {
1820
tests := []struct {

linkedlist/serialization_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,19 @@ TestSerializeAndDeserializeLinkedList tests solution(s) with the following signa
1313
func Serialize(node *Node) string
1414
func Deserialize(stringRepresentation string) *Node
1515
16-
Write a function that turns a linked list into a string representation (Serialize), and then a function
16+
Write a function that turns a linked list of integers into a string representation (Serialize) and a function
1717
that turns that string representation to an actual linked list (Deserialize).
18+
19+
For example consider the following example of a linked list containing numbers 1,2,3:
20+
21+
node1 := &Node{Val: 1}
22+
node2 := &Node{Val: 2}
23+
node3 := &Node{Val: 3}
24+
node1.Next = node2
25+
node2.Next = node3
26+
27+
A string representation of this linked-list should look like 1->2->3. This means that
28+
the node with value 1 is connected to node with value 2, and node with value 2 is connected to node with value 3.
1829
*/
1930
func TestSerializeAndDeserializeLinkedList(t *testing.T) {
2031
tests := []string{

0 commit comments

Comments
 (0)