Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Insert at End Tail Insert in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at End Tail Insert
Create new node
↓
Is list empty?
↓
Set head = new
↓
Traverse until current.next == null
↓
Set current.next = new node
↓
Done
Create a new node, check if list is empty, if yes set head to new node, else traverse to last node and link new node at end.
Execution Sample
DSA Typescript
class Node {
  data: number;
  next: Node | null = null;
  constructor(data: number) { this.data = data; }
}

function insertAtEnd(head: Node | null, data: number): Node {
  const newNode = new Node(data);
  if (head === null) {
    return newNode;
  }
  let current = head;
  while (current.next !== null) {
    current = current.next;
  }
  current.next = newNode;
  return head;
}

// Example usage:
let head: Node | null = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head = insertAtEnd(head, 40);
Inserts a new node with value 40 at the end of the linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=4010 -> 20 -> 30 -> nullnewNode = Node(40)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
2Check if head is null10 -> 20 -> 30 -> nullhead != null (list not empty)Same as above
3Start traversal at head10 -> 20 -> 30 -> nullcurrent = head (Node 10)Same as above with current pointer at 10
4Traverse to next node (20)10 -> 20 -> 30 -> nullcurrent = current.next (Node 20)Same as above with current pointer at 20
5Traverse to next node (30)10 -> 20 -> 30 -> nullcurrent = current.next (Node 30)Same as above with current pointer at 30
6current.next is null, insert new node10 -> 20 -> 30 -> nullcurrent.next = newNode (Node 40)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ data:30│ │ data:40│ │ next:──┼──→ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
7Done10 -> 20 -> 30 -> 40 -> nullNo pointer changesFinal list with new node at end
šŸ’” Traversal ends when current.next is null, new node linked as last node.
Variable Tracker
VariableStartAfter 1After 2After 3After 4Final
headNode(10)Node(10)Node(10)Node(10)Node(10)Node(10)
currentnullnullnullNode(10)Node(20)Node(30)
newNodenullNode(40)Node(40)Node(40)Node(40)Node(40)
list size333334
Key Moments - 3 Insights
Why do we check if head is null before traversal?
Because if head is null, the list is empty and the new node becomes the head directly (see step 2). Otherwise, we must traverse to the end.
Why do we stop traversal when current.next is null, not current itself?
Because current.next being null means current is the last node. We insert the new node by setting current.next to newNode (see step 6).
What happens to the new node's next pointer after insertion?
It remains null (āˆ…) because it is now the last node in the list (see visual state in step 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 5, where is the current pointer?
AAt node with data 20
BAt node with data 30
CAt node with data 40
DAt head (node with data 10)
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 5 in the execution table.
At which step does the list size increase?
AStep 6
BStep 5
CStep 3
DStep 7
šŸ’” Hint
Look at the variable_tracker for 'list size' changes and the execution table pointer changes.
If the list was empty initially, what would be the pointer change at step 2?
Acurrent = head
Bcurrent.next = newNode
Chead = newNode
DNo pointer changes
šŸ’” Hint
Refer to the concept flow and step 2 explanation about empty list handling.
Concept Snapshot
Insert at End (Tail Insert) in Linked List:
- Create new node
- If list empty, set head = new node
- Else traverse to last node (current.next == null)
- Set last node's next = new node
- New node's next = null
- List size increases by 1
Full Transcript
To insert a node at the end of a linked list, first create the new node. Check if the list is empty by seeing if head is null. If empty, set head to the new node. If not, start at head and traverse nodes until you find the last node where next is null. Then set that last node's next pointer to the new node. The new node's next pointer remains null, marking it as the new tail. This process increases the list size by one.

Practice

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

Solution

  1. Step 1: Understand the operation

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

    Adding at the beginning or deleting nodes are different operations.
  3. Final Answer:

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

    Insert at End = Add node at tail [OK]
Hint: Tail insert always adds node after last element [OK]
Common Mistakes:
  • Confusing tail insert with head insert
  • Thinking it deletes nodes
  • Assuming it inserts in the middle
2. Which of the following is the correct TypeScript syntax to create a new node for tail insertion in a singly linked list?
easy
A. const newNode = new Node(value);
B. const newNode = Node(value);
C. let newNode = Node.create(value);
D. newNode = new Node();

Solution

  1. Step 1: Recall TypeScript class instantiation

    To create an instance of a class, use the 'new' keyword with constructor.
  2. Step 2: Check each option

    const newNode = new Node(value); uses 'new Node(value);' which is correct syntax.
  3. Final Answer:

    const newNode = new Node(value); -> Option A
  4. Quick Check:

    Use 'new' keyword to create class instance [OK]
Hint: Use 'new' keyword to create node instances [OK]
Common Mistakes:
  • Omitting 'new' keyword
  • Calling class like a function
  • Using undefined methods like create()
3. Consider the following TypeScript code for inserting at the end of a singly linked list:
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

class LinkedList {
  head: Node | null = null;

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

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

// What is the linked list after these insertions?
medium
A. 10 -> 5 -> 15 -> null
B. 15 -> 10 -> 5 -> null
C. 5 -> 15 -> 10 -> null
D. 5 -> 10 -> 15 -> null

Solution

  1. Step 1: Insert 5 into empty list

    Since head is null, head becomes node with value 5.
  2. Step 2: Insert 10 at end

    Traverse from head (5), next is null, so set next to new node 10.
  3. Step 3: Insert 15 at end

    Traverse from head (5) to 10, next is null, set next to new node 15.
  4. Final Answer:

    5 -> 10 -> 15 -> null -> Option D
  5. Quick Check:

    Nodes added in order at tail [OK]
Hint: Tail insert keeps order of insertion [OK]
Common Mistakes:
  • Assuming insertion at head
  • Mixing node order
  • Not updating next pointers correctly
4. The following TypeScript code attempts to insert a node at the end of a singly linked list but causes an error:
insertAtEnd(value: number) {
  const newNode = new Node(value);
  if (!this.head) {
    this.head = newNode;
  }
  let current = this.head;
  while (current.next) {
    current = current.next;
  }
  current.next = newNode;
}
What is the error in this code?
medium
A. It inserts the new node twice when the list is empty
B. It never updates the head pointer
C. It causes an infinite loop
D. It does not create a new node properly

Solution

  1. Step 1: Analyze empty list case

    If head is null, head is set to newNode, but code continues to traverse and insert again.
  2. Step 2: Identify double insertion

    After setting head, it still traverses and sets current.next = newNode, adding newNode twice.
  3. Final Answer:

    It inserts the new node twice when the list is empty -> Option A
  4. Quick Check:

    Missing return after head assignment causes double insert [OK]
Hint: Return immediately after setting head in empty list [OK]
Common Mistakes:
  • Not returning after head assignment
  • Assuming single insertion always
  • Ignoring traversal logic
5. You want to optimize the tail insertion in a singly linked list to avoid traversing the entire list every time. Which modification will achieve this?
hard
A. Insert new nodes at the head instead of the tail
B. Use a doubly linked list instead of singly linked list
C. Keep a tail pointer that always points to the last node
D. Store the list length and use it to find the last node

Solution

  1. Step 1: Understand traversal cost

    Without a tail pointer, inserting at end requires traversing all nodes, which is slow for long lists.
  2. Step 2: Use tail pointer

    Maintaining a tail pointer lets you directly access the last node and insert in constant time.
  3. Step 3: Evaluate other options

    Inserting at head changes order, doubly linked list adds complexity, length doesn't help find last node efficiently.
  4. Final Answer:

    Keep a tail pointer that always points to the last node -> Option C
  5. Quick Check:

    Tail pointer = O(1) insert at end [OK]
Hint: Tail pointer avoids full list traversal [OK]
Common Mistakes:
  • Thinking length helps find last node
  • Confusing head insertion with tail insertion
  • Ignoring complexity of doubly linked list
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