Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this linked list after insertions?
Consider a singly linked list where we insert nodes with values 10, 20, and 30 at the end. What will be the printed state of the list?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtEnd(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.insertAtEnd(10); list.insertAtEnd(20); list.insertAtEnd(30); list.printList(); } }
Attempts:
2 left
💡 Hint
Think about how nodes are added at the end and how the printList method traverses the list.
✗ Incorrect
Nodes are inserted at the end, so the order is 10, then 20, then 30. The printList method prints each node's data followed by '->', ending with 'null'.
❓ Predict Output
intermediateWhat is the output after deleting the head node?
Given a singly linked list with nodes 5 -> 15 -> 25 -> null, what will be the printed list after deleting the head node?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtEnd(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 deleteHead() { if (head != null) { head = head.next; } } 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.insertAtEnd(5); list.insertAtEnd(15); list.insertAtEnd(25); list.deleteHead(); list.printList(); } }
Attempts:
2 left
💡 Hint
Deleting the head means the second node becomes the new head.
✗ Incorrect
After deleting the head (5), the new head is 15, so the list is 15 -> 25 -> null.
🔧 Debug
advancedWhy does this linked list insertion code throw a NullPointerException?
Examine the insertAtEnd method below. Why does the program throw a NullPointerException when inserting nodes?
DSA Java
void insertAtEnd(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; return; } Node temp = head; while (temp != null) { temp = temp.next; } temp.next = newNode; }
Attempts:
2 left
💡 Hint
Think about when the loop stops and what happens to temp after the loop.
✗ Incorrect
The loop should stop at the last node, which has next == null. Using 'temp != null' causes temp to become null, then temp.next causes NullPointerException.
❓ Predict Output
advancedWhat is the output after reversing this linked list?
Given a linked list 1 -> 2 -> 3 -> null, what is the printed list after applying the reverse method below?
DSA Java
class Node { int data; Node next; Node(int data) { this.data = data; this.next = null; } } class LinkedList { Node head; void insertAtEnd(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 reverse() { Node prev = null; Node current = head; while (current != null) { Node nextNode = current.next; current.next = prev; prev = current; current = nextNode; } head = prev; } 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.insertAtEnd(1); list.insertAtEnd(2); list.insertAtEnd(3); list.reverse(); list.printList(); } }
Attempts:
2 left
💡 Hint
The reverse method changes the direction of the links between nodes.
✗ Incorrect
The reverse method iteratively reverses the pointers, so the list becomes 3 -> 2 -> 1 -> null.
🧠 Conceptual
expertWhat is the time complexity of inserting a node at the end of a singly linked list without a tail pointer?
Consider a singly linked list with n nodes and no tail pointer. What is the time complexity to insert a new node at the end?
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 the head and move through all nodes to reach the end, which takes O(n) time.
