Single Linked List Operations and Binary Trees (ISC Class 12 Computer Science)
Updated 17 October 2026 · Draft, not yet fact-checked
A single linked list is a chain of nodes, each holding data and a reference to the next node; the first node is the head and the last node's next is null. Insertion, deletion and reversal change only references. A binary tree has nodes with at most two children; its traversals are preorder (root, left, right), inorder (left, root, right) and postorder (left, right, root).
Follow the next arrows from head until null; in a tree, preorder visits the root first, inorder in the middle, postorder last.
Illustration drawn for QuestionBuddy.
Understand Single Linked List Operations and Binary Trees
Node and list.
class Node {
int data; Node next;
Node(int d) { data = d; next = null; }
}
class LinkedList {
Node head; // null means the list is empty
boolean isEmpty() { return head == null; }
}Unlike an array, a linked list grows and shrinks as needed and needs no shifting for insertion, but reaching the k-th item means walking from the head.
Traversal. for (Node t = head; t != null; t = t.next) System.out.print(t.data + " ");
Insertion. At the front: Node n = new Node(x); n.next = head; head = n;. At the end: if the list is empty set head = n, else walk to the last node (t.next == null) and set t.next = n. After a given node: n.next = t.next; t.next = n; (in this order).
Deletion. First node: head = head.next;. Other node: find the node t before it and set t.next = t.next.next;. Always test for an empty list first.
Reversal. Three references: prev = null, cur = head; while cur is not null: nxt = cur.next; cur.next = prev; prev = cur; cur = nxt; Finally head = prev.
Extracting an element or sublist. Walk to the required position counting nodes and copy the data (or the references from the start to the end positions) into a new list.
Binary tree (conceptual). Each node has at most two children, called left and right. Terms (with the convention stated here): root is the top node; internal node has at least one child; external node (leaf) has none; degree of a node is its number of children; siblings share a parent; a subtree is a node with all its descendants; depth of a node is the number of edges from the root to it (so the root has depth 0 and level 0); height of a node is the number of edges on the longest path down to a leaf, and the height of the tree is the height of the root; size is the number of nodes. A tree is complete when all levels except possibly the last are full and the last is filled from the left; it is balanced when the heights of the left and right subtrees of every node differ by at most one.
Traversals. Preorder: root, left subtree, right subtree. Inorder: left subtree, root, right subtree. Postorder: left subtree, right subtree, root. Each is defined recursively.
The syllabus asks for the linked list as algorithm and program and for trees conceptually only (no tree programs). Doubly and circular lists are outside the syllabus.
Single Linked List Operations and Binary Trees: key facts
- Linked list basics
Empty list head == null Last node next == null Insert at front n.next = head, head = n Delete first head = head.next - Always check for an empty list first.
- Deletion and insertion after a node
- Delete after t: t.next = t.next.next
- Insert after t: n.next = t.next, then t.next = n
- Order of the two assignments matters
- Reversing the order loses the rest of the list.
- Tree traversals
Preorder root, left, right Inorder left, root, right Postorder left, right, root Defined recursively - Pre and post give the prefix and postfix forms of an expression tree.
- Tree terms
Leaf no children Degree number of children Depth edges from the root Height edges to the deepest leaf Size number of nodes - State the convention used for height and level.
How to answer Single Linked List Operations and Binary Trees questions
Use this routine for list and tree questions.
- 1Draw the list as boxes and arrows with head marked; mark the changed arrows after each operation.
- 2For insertion or deletion, find the node before the position by walking with a temporary reference.
- 3Handle the special cases: empty list, first node, last node.
- 4For reversal, trace prev, cur and nxt in a table.
- 5For a tree, label the root, then find the leaves and the longest path for the height.
- 6For a traversal, apply the rule at every node recursively: do the left subtree completely before the right.
Single Linked List Operations and Binary Trees in real life
The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.
Train coaches
Suppose a train is a list of coaches, each coupled only to the next coach, and a new coach is added at the very front or the very end.
Which coach must you know to add at the front, and which to add at the end?
Try it first, then show the solutionSolution
- To add at the front you need only the engine (the head).
- To add at the end you must walk to the last coach whose next is null.
Answer: Head for the front; the last node for the end.
Memory hook: You enter the chain through the head.
Family tree reading orders
Suppose a document lists a family tree by writing each parent before the children.
Which traversal order does this match?
Try it first, then show the solutionSolution
- The parent (root) comes before the children.
- That is preorder: root, left, right.
Answer: Preorder
Memory hook: Pre = parent first.
Tricks to remember Single Linked List Operations and Binary Trees
Short memory aids. Say each one aloud once, then use it on a question the same day.
Link new first, then attach
Insertion: set the new node's next first, then redirect the old link.
Use it when: Avoiding a lost list.
Pre, in, post = where the root is
Root first, root in the middle, root last.
Use it when: Tree traversals.
Three pointers for reversal
prev, cur, nxt: save next, flip the link, step ahead.
Use it when: Reversing a list.
Leaf = no children
External nodes are the leaves; all others are internal.
Use it when: Tree terms.
Single Linked List Operations and Binary Trees: solved examples
Ordered from board level to HOTS. Try each one before you open the solution.
Example 1: End of a list · 1 mark · MCQBoard level
In a single linked list, the next field of the last node holds
(a) the head (b) null (c) zero (d) the previous node
Try it first, then show the solutionSolution
- The last node points to nothing, which is null.
Answer: (b) null
Example 2: Insertions at both ends · 3 marks · Short answerHarder board level
An empty list receives addEnd(5), addEnd(8), addFront(2), addFront(9), addEnd(4). Show the list after each operation and write the traversal output.
void addFront(int x) {
Node n = new Node(x);
n.next = head;
head = n;
}
void addEnd(int x) {
Node n = new Node(x);
if (head == null) head = n;
else {
Node t = head;
while (t.next != null) t = t.next;
t.next = n;
}
}Try it first, then show the solutionSolution
- addEnd(5): 5. addEnd(8): 5 8. (1 mark)
- addFront(2): 2 5 8. addFront(9): 9 2 5 8. (1 mark)
- addEnd(4) walks to 8 and attaches 4: 9 2 5 8 4. Traversal prints 9 2 5 8 4. (1 mark)
Answer: 9 2 5 8 4
Example 3: Deletion of a value · 4 marks · Short answerHarder board level
Apply delete(5), then delete(9), then delete(7) to the list 9, 2, 5, 8, 4 using the method below, and show the list each time.
void delete(int x) {
if (head == null) return;
if (head.data == x) { head = head.next; return; }
Node t = head;
while (t.next != null && t.next.data != x)
t = t.next;
if (t.next != null)
t.next = t.next.next;
}Try it first, then show the solutionSolution
- delete(5): head is 9, not 5; t moves to 2, whose next is 5, so t.next = 8: list 9 2 8 4. (1.5 marks)
- delete(9): the head matches, so head = head.next: list 2 8 4. (1.5 marks)
- delete(7): t walks to the last node (4) without finding 7, t.next is null, so nothing changes: list 2 8 4. (1 mark)
Answer: 9 2 8 4, then 2 8 4, then 2 8 4
Example 4: Reversing a list · 4 marks · Short answerHarder board level
Trace the reversal of the list 1, 2, 3 using prev, cur and nxt, and give the new list.
void reverse() {
Node prev = null, cur = head, nxt;
while (cur != null) {
nxt = cur.next;
cur.next = prev;
prev = cur;
cur = nxt;
}
head = prev;
}Try it first, then show the solutionSolution
- Start: prev = null, cur = node 1. Round 1: nxt = 2, node 1 now points to null, prev = 1, cur = 2. (1.5 marks)
- Round 2: nxt = 3, node 2 points to 1, prev = 2, cur = 3. (1 mark)
- Round 3: nxt = null, node 3 points to 2, prev = 3, cur = null; head = prev = node 3. (1 mark)
- The list is now 3, 2, 1. (0.5 mark)
Answer: 3, 2, 1
Example 5: Case: reading a binary tree · 4 marks · Case-basedHOTS / competency
Suppose a tree has root A with children B (left) and C (right); B has children D (left) and E (right); C has only a right child F; F has only a left child G. Use the convention that the root has depth 0 and height is counted in edges.
(i) Name the leaves and the internal nodes. (1 mark)
(ii) Give the height of the tree and the depth of G. (1 mark)
(iii) Write the preorder, inorder and postorder traversals. (2 marks)
Try it first, then show the solutionSolution
- (i) Leaves (external nodes): D, E, G. Internal nodes: A, B, C, F.
- (ii) The longest path A, C, F, G has 3 edges, so the height of the tree is 3 and the depth of G is 3. The tree has 7 nodes. It is not complete because C has one child.
- (iii) Preorder: A B D E C F G. Inorder: D B E A C G F. Postorder: D E B G F C A.
Answer: (i) leaves D, E, G; internal A, B, C, F (ii) height 3, depth of G 3 (iii) A B D E C F G; D B E A C G F; D E B G F C A
Where students lose marks in Single Linked List Operations and Binary Trees
The slips examiners see most often, and what to write instead.
Mistake: Assigning the links in the wrong order
If t.next is changed first, the rest of the list is lost.
Wrong working
t.next = n; n.next = t.next;
Right working
n.next = t.next; t.next = n;
Do this: Attach the new node to the rest first.
Usually costs: 1 mark
Mistake: No empty or first-node check
A null head causes NullPointerException.
Wrong working
Node t = head; while (t.next != null) ...
Right working
if (head == null) head = n; else { ... }
Do this: Test head == null at the start.
Usually costs: 1 mark
Mistake: Wrong inorder for a missing child
A missing left child is skipped, not replaced by the root.
Wrong working
Inorder of C with only a right child F: F C
Right working
Inorder of C with only a right child F: C F
Do this: Left (none), then root, then right.
Usually costs: 1 mark
Exam tips
- Draw boxes and arrows and update them after each step; the picture earns marks.
- State the convention you use for height, depth and level of a tree.
- Write the special cases (empty list, deleting the head) in your method.
- Remember: trees are conceptual only, so no tree programs are needed.
- For traversals, write the answer as a single line of node names.
Practice questions on Single Linked List Operations and Binary Trees
Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.
Q1 · 1 mark · MCQBoard level
The traversal that visits the root first is
(a) preorder (b) inorder (c) postorder (d) level order onlyHint
The prefix "pre" means before.
Answer
(a) preorder
Q2 · 2 marks · Short answerBoard level
Give two differences between an array and a single linked list.
Hint
Size and insertion.
Answer
An array has a fixed size and stores elements in consecutive cells with direct access by index; a linked list grows dynamically, its nodes can be anywhere in memory connected by references, and reaching an item needs a walk from the head. Insertion in the middle of an array needs shifting, while a linked list only changes references.
Q3 · 4 marks · Short answerHarder board level
Write a method int count() for the LinkedList class that returns the number of nodes, and another int sum() that returns the sum of the data. Give both for the list 4, 7, 1.
Hint
Use a temporary reference t.
Answer
int count() { int c = 0; for (Node t = head; t != null; t = t.next) c++; return c; } int sum() { int s = 0; for (Node t = head; t != null; t = t.next) s += t.data; return s; }For 4, 7, 1: count() = 3 and sum() = 12.
Q4 · 4 marks · Short answerHarder board level
Write a method insertSorted(int x) that inserts x into a list that is in ascending order, and show the steps for inserting 6 into 2, 5, 8.
Hint
Stop at the node whose next data is not less than x.
Answer
void insertSorted(int x) { Node n = new Node(x); if (head == null || x < head.data) { n.next = head; head = n; return; } Node t = head; while (t.next != null && t.next.data < x) t = t.next; n.next = t.next; t.next = n; }For 6: head 2 is smaller, so it is not the front. t starts at 2; 5 < 6, so t moves to 5; the next data 8 is not less than 6, so stop. n.next = 8 and 5.next = n. The list is 2, 5, 6, 8.
Q5 · 4 marks · Case-basedHOTS / competency
An expression tree has root *, whose left child is + (with leaves A and B) and whose right child is - (with leaves C and D). (i) Write the preorder, inorder and postorder traversals. (ii) Which two give the prefix and postfix forms? (iii) Why does the inorder result need brackets to mean (A + B) * (C - D)?
Hint
Apply the rule at each node.
Answer
(i) Preorder: * + A B - C D. Inorder: A + B * C - D. Postorder: A B + C D - *. (ii) Preorder gives the prefix form and postorder gives the postfix form. (iii) Inorder lists the operators between operands but has lost the tree structure, so without brackets the precedence rules would group B * C first.
Test your recall
Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.
1.What marks the end of a single linked list?Show answer
A node whose next is null.
2.Write the inorder rule.Show answer
Left subtree, root, right subtree.
3.What is a leaf?Show answer
A node with no children (an external node).
4.What does head == null mean?Show answer
The list is empty.
5.How do you delete the first node?Show answer
head = head.next.
6.What is the degree of a node?Show answer
The number of its children.
Single Linked List Operations and Binary Trees: frequently asked questions
What is a linked list?
A linear data structure of nodes, each holding data and a reference to the next node, so the order is kept by references and not by position in memory.
What is the difference between preorder, inorder and postorder traversal?
They differ in when the root is visited: before both subtrees (preorder), between them (inorder) and after them (postorder).
What is the difference between depth and height of a node?
Depth is the distance from the root down to the node; height is the distance from the node down to its deepest leaf. The convention (edges or nodes) should be stated.
Which linked lists are in the ISC syllabus?
Only the single linked list, with algorithms and programs; doubly and circular lists are outside the syllabus, and trees are conceptual only.