Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Practice problems for linked list and recursion

Last Updated on March 12, 2022 by Ria Pathak

### Introduction

Assume the node of the linked list has the following structure:

```struct Node
{
int data;
struct Node* next;
};

```
```struct Node {
int val;
Node* next;
Node(int value){
val = value;
next = NULL;
}
};
```
```class Node:
def __init__(self, val):
self.val = val
self.next = None
```

### Problem 1

```void fun1(Node *head){
}
```
```void fun1(Node *head){
}
```
```def func1(head):
```

Explanation:
When the head of a linked list is passed to the given function as an argument, it will print the value in the head node and call the same function for the next node. This will continue till we reach NULL.

So this will print the whole linked list from beginning to end.

```void fun2(struct Node *head){
}
```
```
}
```
```def func2(head):
```

Explanation:
When the head of a linked list is passed to the given function as an argument, it will call the same function for the next node and after doing so it will print the value in the head node. This will continue till we reach NULL.

So this will print the whole linked list after the current node before printing the current node. Hence the list will be printed in reverse order.

### Problem 3

```void fun3(struct Node* head){
return;
}
}
```
```void fun3(Node* head){
return;
}
}
```
```def func3(head):
return

```

Explanation:
This function checks if the current node is the last node and if it is then adds 1 to it otherwise call the same function recursively for the next node.

### Problem 4

```int fun4(struct Node* head){
}
```
```int fun4(Node* head){
return 0;
}
```
```def func4(head):
return 0

```

Explanation:
It adds 1 for each node that is not null and returns 0 for the empty linked list. In the end, we would have the sum of all 1’s contributed by non-empty nodes and hence the length of the linked list.

### Problem 5

```bool fun5(Node* head, int x){
}

```
```bool fun5(Node* head, int x){
return false;
return true;
}
```
```def func5(head, x):
return False
return True

```

Answer: Checks if there is a node with the value x in the given linked list.

Explanation:
The given function recursively iterates through the linked list and matches the values of nodes with x and returns true if they are equal else when the traversal ends returns false.

### Problem 6

```struct Node* fun6(struct Node* head, int x){
}
```
```Node* fun6(Node* head, int x){

}
```
```def func6(head, x):
```

Answer: Deletes the first occurrence of a node with the value x in the linked list.

Explanation:
The given function iterates recursively through the linked list and matches the value in the nodes with x. If they match, then it returns the whole linked list following the first matching node. In every function call, the next of the current node is updated. And in case of a match, the next pointer of the node just before the first matching node will be updated with the remaining linked list. Hence the first matching node will be removed.

### Problem 7

```struct Node* fun7(struct Node* head, int x){
}
```
```Node* fun7(Node* head, int x){
}
```
```def func7(head, x):