Prashnikaप्रश्निका
Code
About

Linked list visualiser

A singly linked list is a chain of nodes, each holding a value and the address of the next node, with start pointing at the first. Type a starting list and the operations to run, then press Play: new nodes are made and linked in, the pointers start, p, prev, curr and next move along the arrows, a reversal turns the arrows round one by one, and the Java line that is running is lit.

How to use it

  1. 1Type the numbers the list starts with (or none) and the operations to run, e.g. addfirst 5, delete 20, reverse, or tap an example.
  2. 2Press Show me, then Play. New nodes are made and linked in, and the pointers start, p, prev, curr and next move along the arrows.
  3. 3Watch each arrow change and the Java line that is running. Use Back and Next, the slider or the arrow keys to go at your own speed.

Separate with commas: addfirst 5, addlast 9, insertat 2 7 (value 7 becomes node 2), deletefirst, deletelast, delete 9 (the first node holding 9), search 9, traverse, reverse. At most 10 nodes at a time.

The animation needs JavaScript. Every step is written out below.

Result

Result
10 → 20 → 40 (3 nodes)
Every step in words (11 steps)
  1. The list starts as 10 → 20 → 30 → 40.
  2. delete(30) is called: the first node holding 30 will be removed.
  3. start is not null, so look for the value.
  4. start.data is 10, not 30.
  5. p = start: p begins at the first node (10).
  6. p.next holds 20, which is not 30, so p moves on.
  7. p = p.next: p moves to 20.
  8. p.next holds 30: p is the node just before it.
  9. p.next is not null, so the node can be cut out.
  10. p.next = p.next.next: node 20 now skips the node holding 30.
  11. Nothing points at the node holding 30 any more: Java removes it automatically.
Start again

About the Linked list visualiser

What it does

Runs operations on a singly linked list one pointer change at a time. Every node is a box holding a value and an arrow to the next node, start points at the first node and the last node's next is null. A new node is made (shown below the row until it is linked in), the pointers that walk the list are labelled above the node they point at, a deleted node is marked and then removed, and the Java line that is running is lit. A reversal turns the arrows round one by one, with prev, curr and next shown, and then draws the list again from start.

The operations

  • addfirst x, addlast x: add a node at the start or at the end.
  • insertat p x: make x node number p (1 is the first node).
  • deletefirst, deletelast: remove the first or the last node.
  • delete x: remove the first node that holds x.
  • search x, traverse: look for x; visit every node and print its value.
  • reverse: turn every next pointer round.

The limits

A starting list of up to 8 numbers, at most 10 nodes at any time, up to 20 operations, each number from -99 to 999. Deleting from an empty list does nothing and the sentence is shown in red.

Common questions

What is a singly linked list?

A chain of nodes, each holding some data and a reference to the next node. A reference called start (or head) points at the first node and the last node's next is null. Unlike an array it can grow and shrink without moving the other values.

How do you insert a node at the end of a linked list?

Make the new node, then, if start is null, make start point at it; otherwise walk a pointer p along the list until p.next is null and set p.next to the new node.

How do you reverse a singly linked list?

Keep three pointers: prev (null at first), curr (start) and next. For each node save next = curr.next, turn the pointer round with curr.next = prev, then move prev and curr on. When curr is null, set start = prev.

What happens when you delete from an empty linked list?

start is null, so there is no node to delete; the method must check this first (if (start == null) return;) or it would fail with a NullPointerException.

Can I see the code in Python, JavaScript or C++?

Yes. Choose the language in the menu above the code. Only the listing changes: the steps are always worked out from the Java program that ISC uses, and the highlighted line moves to the matching line of the language you chose.

Practise on real ISC questions

More Computer Science tools

All study tools →