Challenge - 5 Problems
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing nodes in this linked list?
Consider the following Java code that pushes nodes to the front of a singly linked list. What will be the printed linked list after all push operations?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } 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.push(10); list.push(20); list.push(30); list.printList(); } }
Attempts:
2 left
💡 Hint
Remember, push adds a new node at the front of the list.
✗ Incorrect
Each push adds a new node at the head. So the last pushed node (30) becomes the first element, followed by 20, then 10.
❓ Predict Output
intermediateWhat is the linked list after these push operations?
Given the following Java code snippet, what will be the final linked list printed?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } 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.push(5); list.push(15); list.push(25); list.push(35); list.printList(); } }
Attempts:
2 left
💡 Hint
New nodes are added at the front, so the last pushed is first.
✗ Incorrect
Push inserts nodes at the head, so the list is reversed order of pushes: 35, 25, 15, 5.
🔧 Debug
advancedWhat error occurs when pushing nodes with this code?
Examine the following Java code that attempts to push nodes into a linked list. What error will it produce when run?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head.next; head = new_node; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.push(1); list.push(2); } }
Attempts:
2 left
💡 Hint
Check what happens when head is null and you access head.next.
✗ Incorrect
Initially head is null, so head.next causes NullPointerException when push is called first time.
🧠 Conceptual
advancedWhat is the time complexity of push operation in a singly linked list?
In a singly linked list, the push operation adds a new node at the front. What is the time complexity of this operation?
Attempts:
2 left
💡 Hint
Push only changes a few pointers at the head.
✗ Incorrect
Push adds a node at the front by changing head pointer and new node's next pointer, which takes constant time.
🚀 Application
expertAfter pushing these nodes, what is the value of the third node?
Using the push method below, nodes with values 7, 14, 21, 28 are pushed in that order. What is the data value of the third node in the linked list?
DSA Java
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void push(int new_data) { Node new_node = new Node(new_data); new_node.next = head; head = new_node; } int getThirdNodeData() { if (head == null || head.next == null || head.next.next == null) { return -1; // less than 3 nodes } return head.next.next.data; } } public class Main { public static void main(String[] args) { LinkedList list = new LinkedList(); list.push(7); list.push(14); list.push(21); list.push(28); System.out.println(list.getThirdNodeData()); } }
Attempts:
2 left
💡 Hint
Remember push adds nodes at the front, so the list order is reversed.
✗ Incorrect
Nodes pushed: 7,14,21,28. List after pushes: 28 -> 21 -> 14 -> 7. Third node is 14.
