Bird
Raised Fist0
DSA Javascriptprogramming

Insert at End Tail Insert in DSA Javascript

Choose your learning style10 modes available
Mental Model
Add a new item at the very end of a chain of items by following the links until the last one.
Analogy: Imagine a line of people holding hands. To add a new person at the end, you walk down the line until you find the last person, then they hold the new person's hand.
head -> 1 -> 2 -> 3 -> null
                 ↑
               tail
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null, insert value 4 at end
Goal: Add the value 4 as the last item in the list
Step 1: start at head node with value 1
head -> [curr->1] -> 2 -> 3 -> null
Why: We begin at the start to find the end
Step 2: move curr to next node with value 2
head -> 1 -> [curr->2] -> 3 -> null
Why: Keep moving forward to reach the last node
Step 3: move curr to next node with value 3
head -> 1 -> 2 -> [curr->3] -> null
Why: Continue until the node points to null (end)
Step 4: set curr.next to new node with value 4
head -> 1 -> 2 -> 3 -> 4 -> null
Why: Attach new node at the end
Result:
head -> 1 -> 2 -> 3 -> 4 -> null
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
  }

  insertAtEnd(value) {
    const newNode = new Node(value);
    if (this.head === null) {
      this.head = newNode;
      return;
    }
    let curr = this.head;
    while (curr.next !== null) {
      curr = curr.next;
    }
    curr.next = newNode;
  }

  printList() {
    let curr = this.head;
    let result = '';
    while (curr !== null) {
      result += curr.value + ' -> ';
      curr = curr.next;
    }
    result += 'null';
    console.log(result);
  }
}

// Driver code
const list = new LinkedList();
list.insertAtEnd(1);
list.insertAtEnd(2);
list.insertAtEnd(3);
list.insertAtEnd(4);
list.printList();
if (this.head === null) { this.head = newNode; return; }
handle empty list by setting head to new node
let curr = this.head;
start traversal from head to find last node
while (curr.next !== null) { curr = curr.next; }
move curr forward until last node (where next is null)
curr.next = newNode;
link new node at the end of the list
OutputSuccess
1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(n) because we may need to visit all n nodes to reach the end
Space: O(1) because we only use a few extra variables regardless of list size
vs Alternative: Compared to inserting at the head which is O(1), tail insert is slower because it requires traversal
Edge Cases
empty list
new node becomes the head directly
DSA Javascript
if (this.head === null) {
      this.head = newNode;
      return;
    }
single element list
new node is linked after the single existing node
DSA Javascript
while (curr.next !== null) {
      curr = curr.next;
    }
When to Use This Pattern
When you need to add an item at the end of a linked list, use tail insert by traversing to the last node and linking the new node there.
Common Mistakes
Mistake: Not checking if the list is empty before traversal
Fix: Add a condition to set head to new node if list is empty
Mistake: Linking new node before reaching the last node
Fix: Traverse until curr.next is null before linking new node
Summary
Adds a new node at the end of a linked list.
Use when you want to keep the existing order and add items at the tail.
The key is to find the last node by following next pointers until null.

Practice

(1/5)
1. What does the "Insert at End (Tail Insert)" operation do in a linked list?
easy
A. Adds a new node at the end of the linked list
B. Removes the first node from the linked list
C. Adds a new node at the beginning of the linked list
D. Deletes the last node from the linked list

Solution

  1. Step 1: Understand the operation

    Insert at End means adding a new node after the last node in the linked list.
  2. Step 2: Compare with other operations

    Other options describe removing or adding at the start, which is different from tail insert.
  3. Final Answer:

    Adds a new node at the end of the linked list -> Option A
  4. Quick Check:

    Insert at End = Add node at tail [OK]
Hint: Tail insert always adds node after the last node [OK]
Common Mistakes:
  • Confusing tail insert with head insert
  • Thinking it removes nodes instead of adding
  • Assuming it inserts in the middle
2. Which of the following is the correct syntax to create a new node and insert it at the end of a singly linked list in JavaScript?
easy
A. let newNode = { data: value }; newNode.next = head;
B. let newNode = { data: value, next: null }; tail.next = newNode; tail = newNode;
C. let newNode = new Node(value); newNode.next = head; head = newNode;
D. let newNode = { value: data, nextNode: null }; head.next = newNode;

Solution

  1. Step 1: Identify correct node creation

    Node should have data and next properties; next set to null for tail.
  2. Step 2: Insert at end

    Set current tail's next to newNode, then update tail to newNode.
  3. Final Answer:

    let newNode = { data: value, next: null }; tail.next = newNode; tail = newNode; -> Option B
  4. Quick Check:

    Correct node creation and tail update [OK]
Hint: Tail insert needs next=null and tail pointer update [OK]
Common Mistakes:
  • Using wrong property names like nextNode
  • Inserting at head instead of tail
  • Not updating tail pointer
3. What will be the output linked list after executing the following code?
class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.head = null;
    this.tail = null;
  }
  insertAtEnd(data) {
    const newNode = new Node(data);
    if (!this.head) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail = newNode;
    }
  }
}

const list = new LinkedList();
list.insertAtEnd(5);
list.insertAtEnd(10);
list.insertAtEnd(15);

let current = list.head;
let result = '';
while(current) {
  result += current.data + ' -> ';
  current = current.next;
}
result += 'null';
console.log(result);
medium
A. 5 -> 10 -> 15 -> null
B. 15 -> 10 -> 5 -> null
C. 5 -> 10 -> null
D. null

Solution

  1. Step 1: Insert nodes at end

    Nodes 5, 10, 15 are inserted sequentially at the tail.
  2. Step 2: Traverse and print list

    Traversal prints nodes in insertion order: 5 -> 10 -> 15 -> null.
  3. Final Answer:

    5 -> 10 -> 15 -> null -> Option A
  4. Quick Check:

    Tail insert preserves order [OK]
Hint: Tail insert keeps order; print from head to tail [OK]
Common Mistakes:
  • Confusing head and tail order
  • Missing last node in output
  • Printing reversed list
4. Find the error in the following tail insert method and select the correct fix:
insertAtEnd(data) {
  const newNode = new Node(data);
  if (this.head === null) {
    this.head = newNode;
  } else {
    this.tail.next = newNode;
  }
  // Missing update of tail pointer
}
medium
A. Change 'this.head = newNode;' to 'this.tail = newNode;' in if block
B. Set 'newNode.next = this.tail;' before insertion
C. Remove the else block completely
D. Add 'this.tail = newNode;' after setting tail.next

Solution

  1. Step 1: Identify missing tail update

    After inserting newNode at tail.next, tail pointer must update to newNode.
  2. Step 2: Fix by adding tail update

    Add 'this.tail = newNode;' after 'this.tail.next = newNode;' to keep tail correct.
  3. Final Answer:

    Add 'this.tail = newNode;' after setting tail.next -> Option D
  4. Quick Check:

    Tail pointer must update after insertion [OK]
Hint: Always update tail pointer after inserting at end [OK]
Common Mistakes:
  • Not updating tail pointer causes broken list
  • Changing head instead of tail in else block
  • Incorrectly setting newNode.next to tail
5. You have a singly linked list with nodes: 1 -> 2 -> 3 -> null and a tail pointer correctly pointing to node 3. You want to insert a new node with value 4 at the end. Which sequence of operations correctly updates the list and tail pointer?
hard
A. Create newNode(4), newNode.next = tail, tail = newNode
B. Create newNode(4), head.next = newNode, tail = newNode
C. Create newNode(4), tail.next = newNode, tail = newNode
D. Create newNode(4), tail = newNode, tail.next = null

Solution

  1. Step 1: Create new node with value 4

    Initialize newNode with data=4 and next=null.
  2. Step 2: Link newNode after current tail and update tail

    Set tail.next = newNode to link, then update tail = newNode to move tail pointer.
  3. Final Answer:

    Create newNode(4), tail.next = newNode, tail = newNode -> Option C
  4. Quick Check:

    Tail insert requires linking and tail update [OK]
Hint: Link new node after tail, then update tail pointer [OK]
Common Mistakes:
  • Linking new node to head instead of tail
  • Setting newNode.next to tail (wrong direction)
  • Updating tail before linking new node
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