Imagine you want to store a list of items but you don't know how many items there will be, and you want to add or remove items often. Why might a linked list be better than an array?
Think about what happens when you add or remove items in the middle of an array versus a linked list.
Linked lists allow easy insertion and deletion without shifting other elements because each item points to the next. Arrays require resizing and moving elements, which is costly.
What will be the printed output after adding nodes 1, 2, and 3 to an empty singly linked list?
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void add(int d) { Node newNode = new Node(d); if (head == null) { head = newNode; } else { 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.add(1); list.add(2); list.add(3); list.printList(); } }
Nodes are added at the end of the list in the order they come.
The add method appends nodes at the end, so the list prints in the order 1, 2, 3 followed by null.
What error will this code cause when trying to add a node to the linked list?
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void add(int d) { Node newNode = new Node(d); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } temp = newNode; } } }
Check how the new node is linked to the existing list.
Assigning temp = newNode only changes the local variable temp, not the next pointer of the last node. So the list does not include the new node.
Given this linked list: 1 -> 2 -> 3 -> null, what will be the output after deleting the node with value 2?
class Node { int data; Node next; Node(int d) { data = d; next = null; } } class LinkedList { Node head; void add(int d) { Node newNode = new Node(d); if (head == null) { head = newNode; } else { Node temp = head; while (temp.next != null) { temp = temp.next; } temp.next = newNode; } } void delete(int d) { if (head == null) return; if (head.data == d) { head = head.next; return; } Node temp = head; while (temp.next != null && temp.next.data != d) { temp = temp.next; } if (temp.next != null) { temp.next = temp.next.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.add(1); list.add(2); list.add(3); list.delete(2); list.printList(); } }
Deleting node 2 removes it from the chain, linking 1 directly to 3.
The delete method removes the node with value 2 by changing the next pointer of node 1 to node 3.
In some real-time systems, why might linked lists be preferred over arrays for managing tasks or events?
Think about how quickly tasks can be added or removed in a system that must respond immediately.
Linked lists allow fast insertion and deletion without moving other elements, which helps maintain quick response times in real-time systems.
