Challenge - 5 Problems
Tail Insert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput after inserting nodes at the tail
What is the printed linked list after inserting 10, 20, and 30 at the tail in order?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtTail(10); list.insertAtTail(20); list.insertAtTail(30); list.printList(); } }
Attempts:
2 left
💡 Hint
Remember that inserting at tail adds new nodes at the end of the list.
✗ Incorrect
The insertAtTail method adds nodes at the end. After inserting 10, 20, and 30, the list is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateOutput after inserting into an initially empty list
What is the output after inserting a single node with value 5 at the tail of an empty linked list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtTail(5); list.printList(); } }
Attempts:
2 left
💡 Hint
Inserting into an empty list sets the head to the new node.
✗ Incorrect
Since the list was empty, the new node with value 5 becomes the head, so the list prints as '5 -> null'.
🔧 Debug
advancedIdentify the bug in tail insertion
What error will occur when running this code that inserts nodes at the tail?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp.next == null) { temp = temp.next; } temp.next = newNode; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtTail(1); list.insertAtTail(2); } }
Attempts:
2 left
💡 Hint
Check the while loop condition carefully.
✗ Incorrect
The condition 'while (temp.next == null)' causes an infinite loop when temp.next is null, leading to NullPointerException.
❓ Predict Output
advancedOutput after multiple tail insertions and one print
What is the output after inserting 1, 2, 3, 4 at tail and then printing the list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtTail(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } void printList() { Node temp = head; while (temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null"); } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertAtTail(1); list.insertAtTail(2); list.insertAtTail(3); list.insertAtTail(4); list.printList(); } }
Attempts:
2 left
💡 Hint
Tail insertion adds nodes at the end in order.
✗ Incorrect
Nodes are added at the tail, so the list prints in insertion order: 1 -> 2 -> 3 -> 4 -> null.
🧠 Conceptual
expertTime complexity of tail insertion in singly linked list without tail pointer
What is the time complexity of inserting a node at the tail of a singly linked list when you do NOT maintain a tail pointer?
Attempts:
2 left
💡 Hint
Think about how you find the last node without a tail pointer.
✗ Incorrect
Without a tail pointer, you must start at head and traverse all nodes to reach the tail, which takes O(n) time per insertion.
