# Practice problems for linked list and recursion

### Introduction

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

```
struct Node {
int val;
Node* next;
Node(int value){
val = value;
next = NULL;
}
};

```

### Problem 1

```
cout<val<<" ";
}

```

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.

### Problem 2

```
cout<val<<" ";
}

```

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

```
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

```
}c
```

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

```
}

```

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

```

}
```

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.

```