Skip to content

Instantly share code, notes, and snippets.

@ezek-iel
Last active August 30, 2026 03:13
Show Gist options
  • Select an option

  • Save ezek-iel/5d81e880e1f9d32d26b4e7dad96fbbb7 to your computer and use it in GitHub Desktop.

Select an option

Save ezek-iel/5d81e880e1f9d32d26b4e7dad96fbbb7 to your computer and use it in GitHub Desktop.
012 Rule
package main
import (
"fmt"
"strings"
)
type Node[T comparable] struct {
data T
next *Node[T]
}
type LinkedList[T comparable] struct {
head *Node[T]
tail *Node[T]
}
func NewLinkedList[T comparable]() *LinkedList[T] {
return &LinkedList[T]{}
}
func (l *LinkedList[T]) Insert(node *Node[T]) {
current := l.head
if current == nil {
l.head = node
l.tail = node
return
}
l.tail.next = node
l.tail = node
}
func (l *LinkedList[T]) String() string {
var builder strings.Builder
current := l.head
for current != nil {
fmt.Fprintf(&builder, "Node(%v) ", current.data)
current = current.next
}
return builder.String()
}
func (l *LinkedList[T]) InsertAfter(pos *Node[T], node *Node[T]) {
current := l.head
for current.next != nil {
if current == pos {
node.next = current.next
current.next = node
return
}
current = current.next
}
//If the position is in the last node.
if current == pos {
node.next = current.next
current.next = node
l.tail = node
}
}
func (l *LinkedList[T]) Delete(node *Node[T]) {
current := l.head
if current == node {
l.head = current.next
return
}
for current.next != nil {
if current.next == node {
if current.next.next == nil {
l.tail = current
}
nxt := current.next.next
current.next = nxt
}
}
}
func (l *LinkedList[T]) FromEnd(index int) *Node[T] {
if l.head == nil {
return nil
}
fast := l.head
slow := l.head
for range index {
if fast == nil {
return nil
}
fast = fast.next
}
if fast == nil {
return nil
}
for fast.next != nil {
fast = fast.next
slow = slow.next
}
return slow
}
func FromCollection[T comparable](collection []T) *LinkedList[T] {
l1 := NewLinkedList[T]()
for _, val := range collection {
l1.Insert(&Node[T]{data: val})
}
return l1
}
//We are specifically using ints because its easy to compare
func InsertIntoSortedLinkedList(l *LinkedList[int], data int) {
newNode := &Node[int]{data: data}
//0 items in the collection
if l.head == nil {
l.head = newNode
return
}
//This handles 1 item in the collection as well as
//When the data is smaller than other items in the list
if l.head.data > data {
newNode.next = curr
l.head = newNode
return
}
curr := l.head
for curr != nil {
if curr.next.data > data {
newNode.next = curr.next
curr.next = newNode
return
}
curr = curr.next
}
//This handles 1 item in the collection as well as
//when the node is bigger than other nodes in the list
curr.next = newNode
}
package main
import (
"strings"
"testing"
)
func TestLinkedList(t *testing.T) {
l1 := NewLinkedList[int]()
// Create nodes explicitly and hold onto their references
n1 := &Node[int]{data: 34}
n2 := &Node[int]{data: 45}
n3 := &Node[int]{data: 67}
l1.Insert(n1)
l1.Insert(n2)
l1.Insert(n3)
// Head and tail checks now compare pointers directly
if l1.head != n1 {
t.Errorf("Expected head to be n1 (data: %v), got %v", n1.data, l1.head.data)
}
if l1.tail != n3 {
t.Errorf("Expected tail to be n3 (data: %v), got %v", n3.data, l1.tail.data)
}
expected := "Node(34) Node(45) Node(67)"
if strings.TrimSpace(l1.String()) != strings.TrimSpace(expected) {
t.Errorf("Expected %v, got %v", expected, l1.String())
}
// Delete head using its exact reference
l1.Delete(n1)
expected = "Node(45) Node(67)"
if strings.TrimSpace(l1.String()) != strings.TrimSpace(expected) {
t.Errorf("Expected %v, got %v", expected, l1.String())
}
if l1.tail != n3 {
t.Errorf("Expected tail to be n3 (data: 67), got %v", l1.tail.data)
}
// Delete tail using its exact reference
l1.Delete(n3)
if l1.tail != n2 {
t.Errorf("Expected tail to be n2 (data: 45), got %v", l1.tail.data)
}
expected = "Node(45)"
if strings.TrimSpace(l1.String()) != strings.TrimSpace(expected) {
t.Errorf("Expected %v, got %v", expected, l1.String())
}
// ---- InsertAfter tests ----
l2 := NewLinkedList[int]()
// Store all nodes
n23 := &Node[int]{data: 23}
n50 := &Node[int]{data: 50}
n21 := &Node[int]{data: 21}
n293 := &Node[int]{data: 293}
n109 := &Node[int]{data: 109}
l2.Insert(n23)
l2.Insert(n50)
l2.Insert(n21)
l2.Insert(n293)
l2.Insert(n109)
// Insert after n21
n43 := &Node[int]{data: 43}
l2.InsertAfter(n21, n43)
expected = "Node(23) Node(50) Node(21) Node(43) Node(293) Node(109)"
if strings.TrimSpace(l2.String()) != strings.TrimSpace(expected) {
t.Errorf("Expected %v, got %v", expected, l2.String())
}
// Insert after n293
n57 := &Node[int]{data: 57}
l2.InsertAfter(n293, n57)
// Insert after n109
n345 := &Node[int]{data: 345}
l2.InsertAfter(n109, n345)
if l2.tail != n345 {
t.Errorf("Expected tail to be n345 (data: 345), got %v", l2.tail.data)
}
// FromEnd: m=0 is last element, m=3 is 4th from last
// Order: 23(n23), 50(n50), 21(n21), 43(n43), 293(n293), 57(n57), 109(n109), 345(n345)
// Index 0=345, 1=109, 2=57, 3=293
val := l2.FromEnd(3)
if val != n293 {
t.Errorf("Expected node n293 (data: 293), got %v", val.data)
}
}
func TestInsertIntoSortedLinkedList(t *testing.T) {
n1 := &Node[int]{data: 16}
n2 := &Node[int]{data: 23}
n3 := &Node[int]{data: 39}
n4 := &Node[int]{data: 40}
l1 := NewLinkedList[int]()
l1.Insert(n1)
l1.Insert(n2)
l1.Insert(n3)
l1.Insert(n4)
InsertIntoSortedLinkedList(l1, 30)
expected := "Node(16) Node(23) Node(30) Node(39) Node(40)"
got := l1.String()
if strings.TrimSpace(expected) != strings.TrimSpace(got) {
t.Errorf("expected %s, got %s instead", expected, got)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment