Stage 1 · Code
Linked Lists
Singly Linked List
Node structure, insertion, deletion, and traversal.
5 min readMastering Data Structures & Algorithms for Software Engineering InterviewsCode
Node Structure
Singly linked list — each node holds a value and a next pointer
1
→
2
→
3
→
4
→
5
→
─
null
A singly linked list node contains data and a pointer to the next node. The list maintains a head pointer. In Go, we use a struct with a pointer to itself.
Gosingly-linked-list-node-and-list.go
31 linesLn 1, Col 1Go
ListNode has Val and Next. Next is *ListNode (pointer to same type). Nil Next means end of list.
Basic Operations
Core operations: insert at head/tail, delete by value, search. All require pointer manipulation.
Goinsert,-delete,-search.go
45 linesLn 1, Col 1Go
Head insertion is O(1) - new node points to old head. Tail insertion needs traversal. Deletion uses 'look-ahead' pattern: check curr.Next to modify curr.Next.
Traversal Patterns
| Pattern | Code | Use Case |
|---|---|---|
| Simple | for head != nil { ... head = head.Next } | Read-only, search, print |
| With prev | prev, curr := head, head.Next for curr != nil { ... prev, curr = curr, curr.Next } | Deletion, modification |
| Two pointers | slow, fast := head, head for fast != nil && fast.Next != nil { ... } | Middle, cycle detection |
Common Problems
- Reverse list: Iterative (3 pointers) or recursive.
- Merge two sorted lists: Dummy head + tail pointer.
- Detect cycle: Fast/slow pointers (Floyd's).
- Find middle: Fast/slow, slow ends at middle.
- Remove Nth from end: Fast pointer n steps ahead, then move both.
- Palindrome check: Find middle, reverse second half, compare.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.