|
| 1 | +package main |
| 2 | + |
| 3 | +import "fmt" |
| 4 | + |
| 5 | +type Node struct { |
| 6 | + Value int |
| 7 | + Next *Node |
| 8 | +} |
| 9 | + |
| 10 | +type SinglyLinkedList struct { |
| 11 | + Head *Node |
| 12 | +} |
| 13 | + |
| 14 | +func (list *SinglyLinkedList) AddLeft(value int) { |
| 15 | + node := Node{Value: value} |
| 16 | + |
| 17 | + if list.Head == nil { |
| 18 | + list.Head = &node |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + node.Next = list.Head |
| 23 | + list.Head = &node |
| 24 | +} |
| 25 | + |
| 26 | +func (list *SinglyLinkedList) AddRight(value int) { |
| 27 | + node := Node{Value: value} |
| 28 | + |
| 29 | + if list.Head == nil { |
| 30 | + list.Head = &node |
| 31 | + return |
| 32 | + } |
| 33 | + |
| 34 | + pointer := list.Head |
| 35 | + |
| 36 | + for pointer.Next != nil { |
| 37 | + pointer = pointer.Next |
| 38 | + } |
| 39 | + |
| 40 | + pointer.Next = &node |
| 41 | +} |
| 42 | + |
| 43 | +func (list *SinglyLinkedList) RemoveLeft() { |
| 44 | + if list.Head == nil { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + list.Head = list.Head.Next |
| 49 | +} |
| 50 | + |
| 51 | +func (list *SinglyLinkedList) RemoveRight() { |
| 52 | + if list.Head == nil { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + if list.Head.Next == nil { |
| 57 | + list.Head = nil |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + pointer := list.Head |
| 62 | + |
| 63 | + for pointer.Next.Next != nil { |
| 64 | + pointer = pointer.Next |
| 65 | + } |
| 66 | + |
| 67 | + pointer.Next = nil |
| 68 | +} |
| 69 | + |
| 70 | +func (list *SinglyLinkedList) FindAt(idx int) *Node { |
| 71 | + pointer := list.Head |
| 72 | + |
| 73 | + for i := 0; i < idx; i++ { |
| 74 | + pointer = pointer.Next |
| 75 | + if pointer == nil { |
| 76 | + return nil |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return pointer |
| 81 | +} |
| 82 | + |
| 83 | +func (list *SinglyLinkedList) AddAt(idx, value int) { |
| 84 | + if idx == 0 { |
| 85 | + list.AddLeft(value) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + prevNode := list.FindAt(idx - 1) |
| 90 | + |
| 91 | + if prevNode == nil { |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + node := Node{Value: value} |
| 96 | + |
| 97 | + node.Next = prevNode.Next |
| 98 | + prevNode.Next = &node |
| 99 | +} |
| 100 | + |
| 101 | +func (list *SinglyLinkedList) RemoveAt(idx int) { |
| 102 | + if idx == 0 { |
| 103 | + list.RemoveLeft() |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + prevNode := list.FindAt(idx - 1) |
| 108 | + |
| 109 | + if prevNode == nil { |
| 110 | + return |
| 111 | + } |
| 112 | + |
| 113 | + prevNode.Next = prevNode.Next.Next |
| 114 | +} |
| 115 | + |
| 116 | +func (list *SinglyLinkedList) ToSlice() []int { |
| 117 | + var slice []int |
| 118 | + |
| 119 | + for pointer := list.Head; pointer != nil; pointer = pointer.Next { |
| 120 | + slice = append(slice, pointer.Value) |
| 121 | + } |
| 122 | + |
| 123 | + return slice |
| 124 | +} |
| 125 | + |
| 126 | +func main() { |
| 127 | + list := SinglyLinkedList{} |
| 128 | + |
| 129 | + fmt.Println("AddLeft(1, 2)") |
| 130 | + list.AddLeft(1) |
| 131 | + list.AddLeft(2) |
| 132 | + |
| 133 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 134 | + |
| 135 | + fmt.Println("AddRight(3, 4)") |
| 136 | + list.AddRight(3) |
| 137 | + list.AddRight(4) |
| 138 | + |
| 139 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 140 | + |
| 141 | + fmt.Println("RemoveLeft()") |
| 142 | + list.RemoveLeft() |
| 143 | + |
| 144 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 145 | + |
| 146 | + fmt.Println("RemoveRight()") |
| 147 | + list.RemoveRight() |
| 148 | + |
| 149 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 150 | + |
| 151 | + fmt.Println("AddAt(1, 5)") |
| 152 | + list.AddAt(1, 5) |
| 153 | + |
| 154 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 155 | + |
| 156 | + fmt.Println("RemoveAt(2)") |
| 157 | + list.RemoveAt(2) |
| 158 | + |
| 159 | + fmt.Println("list.toSlice(): ", list.ToSlice()) |
| 160 | + |
| 161 | + fmt.Println("FindAt(0)") |
| 162 | + node := list.FindAt(0) |
| 163 | + fmt.Println("node.Value: ", node.Value) |
| 164 | + |
| 165 | +} |
0 commit comments