Bird
Raised Fist0
DSA Goprogramming

Insert at Middle Specific Position in DSA Go

Choose your learning style10 modes available
Mental Model
To insert a new item in the middle of a list, we find the spot and link the new item there without breaking the chain.
Analogy: Imagine a train where you want to add a new carriage between two existing carriages without disconnecting the train.
Head -> 1 -> 2 -> 3 -> 4 -> null
                 ↑ insert here
Dry Run Walkthrough
Input: list: 1 -> 2 -> 4 -> 5 -> null, insert value 3 at position 3
Goal: Insert the value 3 at the 3rd position so the list becomes 1 -> 2 -> 3 -> 4 -> 5 -> null
Step 1: start at head node with value 1
1 -> 2 -> 4 -> 5 -> null ↑
Why: We begin from the start to find the correct position
Step 2: move to next node with value 2
1 -> 2 -> 4 -> 5 -> null ↑
Why: We move forward to reach position before insertion point
Step 3: create new node with value 3 and link it after node 2
1 -> 2 -> 3 -> 4 -> 5 -> null ↑
Why: Insert new node by adjusting pointers to keep list connected
Result:
1 -> 2 -> 3 -> 4 -> 5 -> null
Annotated Code
DSA Go
package main

import "fmt"

type Node struct {
	value int
	next  *Node
}

// insertAtPosition inserts a new node with given value at the given position (1-based)
func insertAtPosition(head *Node, value int, position int) *Node {
	if position == 1 {
		newNode := &Node{value: value, next: head}
		return newNode // new head
	}
	current := head
	count := 1
	// move to node before insertion point
	for current != nil && count < position-1 {
		current = current.next
		count++
	}
	if current == nil {
		return head // position out of bounds, no change
	}
	newNode := &Node{value: value, next: current.next}
	current.next = newNode
	return head
}

// printList prints the linked list
func printList(head *Node) {
	current := head
	for current != nil {
		fmt.Printf("%d", current.value)
		if current.next != nil {
			fmt.Printf(" -> ")
		}
		current = current.next
	}
	fmt.Println(" -> null")
}

func main() {
	head := &Node{value: 1}
	head.next = &Node{value: 2}
	head.next.next = &Node{value: 4}
	head.next.next.next = &Node{value: 5}

	head = insertAtPosition(head, 3, 3)
	printList(head)
}
if position == 1 {
handle insertion at head by creating new node pointing to current head
for current != nil && count < position-1 {
advance current to node before insertion position
newNode := &Node{value: value, next: current.next}
create new node linking to next node after current
current.next = newNode
link current node to new node to insert in middle
OutputSuccess
1 -> 2 -> 3 -> 4 -> 5 -> null
Complexity Analysis
Time: O(n) because we may need to traverse up to n nodes to reach the insertion point
Space: O(1) because we only create one new node and use constant extra space
vs Alternative: Compared to inserting at head or tail which can be O(1), inserting in the middle requires traversal making it O(n)
Edge Cases
insert at position 1 (head)
new node becomes the new head
DSA Go
if position == 1 {
insert at position greater than list length
list remains unchanged because position is invalid
DSA Go
if current == nil {
insert into empty list at position 1
new node becomes the head of the list
DSA Go
if position == 1 {
When to Use This Pattern
When you need to add an element at a specific spot inside a linked list, use the insert at position pattern to adjust pointers carefully.
Common Mistakes
Mistake: Not handling insertion at the head (position 1) separately
Fix: Add a condition to create a new head node when position is 1
Mistake: Not checking if position is out of bounds causing nil pointer errors
Fix: Check if current is nil before insertion and return original list if so
Summary
Inserts a new node at a given position inside a linked list by adjusting pointers.
Use when you want to add an element anywhere except just at the start or end.
The key is to stop at the node before the insertion point and link the new node there.

Practice

(1/5)
1. What does inserting a node at a specific middle position in a linked list mean?
easy
A. Adding a new node only at the start of the list
B. Adding a new node only at the end of the list
C. Adding a new node exactly between two existing nodes at a given position
D. Removing a node from the middle of the list

Solution

  1. Step 1: Understand linked list positions

    Positions in a linked list start from 1 at the head and increase by 1 for each node.
  2. Step 2: Define middle insertion

    Inserting at a middle position means placing a new node between two existing nodes at that position.
  3. Final Answer:

    Adding a new node exactly between two existing nodes at a given position -> Option C
  4. Quick Check:

    Middle insertion = Insert between nodes [OK]
Hint: Middle insertion means placing node between two nodes [OK]
Common Mistakes:
  • Confusing insertion with deletion
  • Thinking insertion only happens at start or end
  • Ignoring position indexing starting at 1
2. Which Go code snippet correctly inserts a new node at position 3 in a singly linked list?
easy
A. temp := head for i := 1; i < 2; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode
B. temp := head for i := 0; i < 3; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode
C. temp := head for i := 1; i <= 3; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode
D. temp := head for i := 1; i < 3; i++ { temp = temp.next } newNode.next = temp.next temp.next = newNode

Solution

  1. Step 1: Loop to node before position 3

    To insert at position 3, loop until position 2 (i < 2) to get node before insertion point.
  2. Step 2: Adjust pointers correctly

    Set newNode.next to temp.next, then temp.next to newNode to insert newNode at position 3.
  3. Final Answer:

    Code snippet in option A correctly inserts at position 3 -> Option A
  4. Quick Check:

    Loop to position-1, then insert [OK]
Hint: Loop to position-1 node before inserting [OK]
Common Mistakes:
  • Looping too many times and skipping nodes
  • Setting pointers in wrong order causing lost nodes
  • Using wrong loop conditions (<= instead of <)
3. What will be the linked list after inserting value 99 at position 2 in the list 10 -> 20 -> 30 -> null using this Go code?
type Node struct {
  val  int
  next *Node
}

head := &Node{10, &Node{20, &Node{30, nil}}}
newNode := &Node{99, nil}
temp := head
for i := 1; i < 1; i++ {
  temp = temp.next
}
newNode.next = temp.next
temp.next = newNode
medium
A. 10 -> 99 -> 20 -> 30 -> null
B. 99 -> 10 -> 20 -> 30 -> null
C. 10 -> 20 -> 99 -> 30 -> null
D. 10 -> 20 -> 30 -> 99 -> null

Solution

  1. Step 1: Loop runs zero times to position 1 node

    The loop condition i < 1 means no iteration; temp stays at head (value 10).
  2. Step 2: Insert newNode after temp

    newNode.next points to temp.next (node with 20), then temp.next points to newNode, inserting 99 after 10.
  3. Final Answer:

    10 -> 99 -> 20 -> 30 -> null -> Option A
  4. Quick Check:

    Insert after position 1 = position 2 [OK]
Hint: Loop to node before position, then insert new node [OK]
Common Mistakes:
  • Misunderstanding loop range causing wrong insertion
  • Confusing head node position as 0 instead of 1
  • Incorrect pointer assignments losing nodes
4. Identify the error in this Go code that tries to insert a node at position 4 in a linked list of length 3:
temp := head
for i := 1; i <= 4; i++ {
  temp = temp.next
}
newNode.next = temp.next
temp.next = newNode
medium
A. New node is not created before insertion
B. Pointers are assigned in wrong order causing lost nodes
C. Position 4 is invalid for a list of length 3
D. Loop runs too many times causing temp to be nil and panic

Solution

  1. Step 1: Analyze loop iterations

    The loop runs from i=1 to i=4 inclusive, moving temp 4 times forward.
  2. Step 2: Check list length and temp value

    List length is 3, so after 3 moves temp becomes nil; moving one more causes panic on temp.next.
  3. Final Answer:

    Loop runs too many times causing temp to be nil and panic -> Option D
  4. Quick Check:

    Loop beyond list length = nil pointer error [OK]
Hint: Loop only to position-1 within list length [OK]
Common Mistakes:
  • Looping beyond list length causing runtime errors
  • Not checking for nil before accessing next
  • Confusing <= with < in loop condition
5. Given a singly linked list and a position k, write a Go function to insert a new node with value v at position k. If k is greater than the list length + 1, insert at the end. What will be the list after inserting 50 at position 5 in 10 -> 20 -> 30 -> 40 -> null?
hard
A. 10 -> 20 -> 50 -> 30 -> 40 -> null
B. 10 -> 20 -> 30 -> 40 -> 50 -> null
C. 50 -> 10 -> 20 -> 30 -> 40 -> null
D. 10 -> 20 -> 30 -> 50 -> 40 -> null

Solution

  1. Step 1: Check position against list length

    List length is 4; position 5 is length+1, so insert at end.
  2. Step 2: Insert new node at end

    Traverse to last node (40), set its next to new node with value 50, new node next is nil.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> 50 -> null -> Option B
  4. Quick Check:

    Position > length = insert at end [OK]
Hint: If position too big, insert at list end [OK]
Common Mistakes:
  • Trying to insert beyond list length without appending
  • Misplacing new node in middle instead of end
  • Not handling nil next pointer at list end
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard