Data Structure Linked Lists Practice Questions with Solutions

Introductions

Linked lists are an important data structure for understanding how elements can be connected using nodes. In these practice questions, you will work with creating nodes, connecting nodes, traversing a linked list, inserting elements, searching for values, counting nodes, and finding the last node. The examples use JavaScript and focus on practical problem-solving so beginners can understand how linked lists work step by step. Data Structure Linked Lists practice questions with solutions help to understand the concepts.

Question 1: Create and Connect Two Nodes

Question

Create two nodes containing 10 and 20, connect them, and print both values.

Solution

A linked list is made of nodes. Each node stores:

Data
Next

We can create a simple node using a JavaScript class:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

Now create two nodes:

let first = new Node(10);
let second = new Node(20);

Initially:

10 → null

20 → null

Now connect the first node to the second:

first.next = second;

The linked list becomes:

10 → 20 → null

To print the values:

console.log(first.data);
console.log(first.next.data);

Output

10
20

Answer

The two nodes are successfully connected:

10 → 20 → null

Question 2: Traverse a Linked List

Question

Create the following linked list and print every element:

10 → 20 → 30 → 40 → null

Solution

First, create a Node class:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

Create the nodes:

let head = new Node(10);
let second = new Node(20);
let third = new Node(30);
let fourth = new Node(40);

Connect them:

head.next = second;
second.next = third;
third.next = fourth;

Now traverse the list.

We start from head:

let current = head;

while (current !== null) {
    console.log(current.data);
    current = current.next;
}

The traversal works like this:

current = 10
        ↓
current = 20
        ↓
current = 30
        ↓
current = 40
        ↓
current = null

When current becomes null, traversal stops.

Output

10
20
30
40

Answer

The linked list is traversed from the head node to the last node.


Question 3: Count the Number of Nodes

Question

Count the number of nodes in this linked list:

5 → 15 → 25 → 35 → 45 → null

Solution

Create the linked list:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(5);
head.next = new Node(15);
head.next.next = new Node(25);
head.next.next.next = new Node(35);
head.next.next.next.next = new Node(45);

Now start a counter:

let count = 0;
let current = head;

Traverse the list and increase the counter for every node:

while (current !== null) {
    count++;
    current = current.next;
}

The counting happens like this:

5  → count = 1
15 → count = 2
25 → count = 3
35 → count = 4
45 → count = 5

Output

5

Answer

The linked list contains 5 nodes.


Question 4: Search for an Element

Question

Search for the value 30 in this linked list:

10 → 20 → 30 → 40 → null

Print "Found" if the value exists.

Solution

We need to check each node one by one.

Create the list:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);

Set the target:

let target = 30;
let current = head;
let found = false;

Now traverse:

while (current !== null) {
    if (current.data === target) {
        found = true;
        break;
    }

    current = current.next;
}

Finally:

if (found) {
    console.log("Found");
} else {
    console.log("Not Found");
}

The search checks:

10 → Not Found
20 → Not Found
30 → Found

Output

Found

Answer

The value 30 exists in the linked list.


Question 5: Insert a Node at the Beginning

Question

Given this linked list:

20 → 30 → 40 → null

Insert 10 at the beginning.

Solution

The current head is:

20

We create a new node:

let newNode = new Node(10);

Initially:

10 → null

We need to connect the new node to the existing head:

newNode.next = head;

Then make the new node the head:

head = newNode;

The complete logic is:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(20);
head.next = new Node(30);
head.next.next = new Node(40);

let newNode = new Node(10);

newNode.next = head;
head = newNode;

let current = head;

while (current !== null) {
    console.log(current.data);
    current = current.next;
}

The links change from:

20 → 30 → 40 → null

to:

10 → 20 → 30 → 40 → null

Output

10
20
30
40

Answer

10 is successfully inserted at the beginning of the linked list.


Question 6: Insert a Node at the End

Question

Given:

10 → 20 → 30 → null

Insert 40 at the end.

Solution

Create the new node:

let newNode = new Node(40);

The new node initially contains:

40 → null

We need to find the last node.

Start from head:

let current = head;

while (current.next !== null) {
    current = current.next;
}

When the loop finishes, current points to:

30

Now connect the new node:

current.next = newNode;

The complete code is:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);

let newNode = new Node(40);

let current = head;

while (current.next !== null) {
    current = current.next;
}

current.next = newNode;

current = head;

while (current !== null) {
    console.log(current.data);
    current = current.next;
}

The list changes from:

10 → 20 → 30 → null

to:

10 → 20 → 30 → 40 → null

Output

10
20
30
40

Answer

40 is successfully inserted at the end of the linked list.


Question 7: Find the Last Node

Question

Find the last node of this linked list:

100 → 200 → 300 → 400 → null

Solution

The last node is the node whose next value is null.

Create the list:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(100);
head.next = new Node(200);
head.next.next = new Node(300);
head.next.next.next = new Node(400);

Start from the head:

let current = head;

Move forward while the next node exists:

while (current.next !== null) {
    current = current.next;
}

Let’s trace:

100 → 200 → 300 → 400

At 400:

400.next = null

Therefore, 400 is the last node.

Output

400

Answer

The last node contains 400.


Question 8: Find the Sum of All Nodes

Question

Find the sum of all values in this linked list:

5 → 10 → 15 → 20 → null

Solution

Start with:

let sum = 0;

Now traverse the linked list.

while (current !== null) {
    sum += current.data;
    current = current.next;
}

The calculation is:

5 + 10 = 15
15 + 15 = 30
30 + 20 = 50

Complete code:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(5);
head.next = new Node(10);
head.next.next = new Node(15);
head.next.next.next = new Node(20);

let sum = 0;
let current = head;

while (current !== null) {
    sum += current.data;
    current = current.next;
}

console.log(sum);

Output

50

Answer

The sum of all nodes is 50.


Question 9: Delete the First Node

Question

Given:

10 → 20 → 30 → 40 → null

Delete the first node.

Solution

The first node is represented by head.

Initially:

head
 ↓
10 → 20 → 30 → 40 → null

To remove 10, we move the head to the second node.

head = head.next;

Before:

head → 10 → 20 → 30 → 40 → null

After:

head → 20 → 30 → 40 → null

Complete code:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);

head = head.next;

let current = head;

while (current !== null) {
    console.log(current.data);
    current = current.next;
}

Output

20
30
40

Answer

The first node containing 10 has been removed.


Question 10: Find the Middle Node

Question

Find the middle node of this linked list:

10 → 20 → 30 → 40 → 50 → null

Solution

For this problem, we can use two pointers:

slow
fast

The slow pointer moves one node at a time.

The fast pointer moves two nodes at a time.

Start:

slow → 10
fast → 10

After one step:

slow → 20
fast → 30

After another step:

slow → 30
fast → 50

The fast pointer cannot move two more nodes.

Therefore, the slow pointer is at the middle node.

Code:

class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

let head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);

let slow = head;
let fast = head;

while (fast !== null && fast.next !== null) {
    slow = slow.next;
    fast = fast.next.next;
}

console.log(slow.data);

The pointers work like this:

Step 1:
Slow → 20
Fast → 30

Step 2:
Slow → 30
Fast → 50

So the slow pointer reaches the middle.

Output

30

Answer

The middle node contains 30.

Key Takeaways

  • A linked list is made up of nodes connected through links.
  • A node generally contains data and a reference to the next node.
  • The first node is called the head.
  • The last node points to null in a singly linked list.
  • Traversal means visiting nodes one by one from the head.
  • A linked list can be searched by checking each node.
  • A new node can be inserted at the beginning by changing the head.
  • A node can be inserted at the end by finding the current last node.
  • The first node can be removed by moving head to head.next.
  • The middle node can be found efficiently using slow and fast pointers.
  • Traversing a linked list generally takes O(n) time.
  • Inserting at the beginning of a singly linked list can be done in O(1) time.

FAQs

What is a linked list in data structures?

A linked list is a data structure made of nodes where each node stores data and a reference to another node.

What is the head of a linked list?

The head is the first node of a linked list. It provides the starting point for traversing the list.

What does the last node of a singly linked list contain?

The last node normally has its next reference set to null, indicating that there are no more nodes.

How do I traverse a linked list?

Start from the head and repeatedly move to the next node until the current node becomes null.

What is the time complexity of searching a linked list?

Searching a singly linked list generally takes O(n) time in the worst case because every node may need to be checked.

Why is insertion at the beginning of a linked list fast?

Only the head reference and the new node’s next reference need to be changed. Therefore, insertion at the beginning generally takes O(1) time.

What is the difference between an array and a linked list?

An array stores elements in an indexed collection, while a linked list stores elements in nodes connected through links. Arrays provide direct index-based access, while linked lists generally require traversal to reach a particular position.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top