## Introduction

The linked list data structure is a crucial topic to prepare for technical rounds. You can see a variety of ticklish and straightforward questions on this topic. This article will introduce you to the basic problem of a linked list.

To reverse a linked list, you should be clear in your head about what a linked list is and how it is implemented in the Java language.

**Input 1: **

1 -> 2 -> 3 -> 4 -> 5 -> NULL

**Output 1:**

NULL <- 1 <- 2 <- 3 <- 4 <- 5

**Input 2:**

1 -> NULL

**Output 2:**

1 -> NULL

Recommended: Do try the Problem yourself before moving on to the solution

## Approach 1(Iterative Approach)

We will first discuss the Iterative Approach to reverse the Linked List. We will be maintaining the following three pointers:-

- current: This pointer will point to the current node we are at.
- prev: This pointer will point to the previous node of the current node. We will assume the previous of the Head node to be NULL.
- next: This pointer will point to the next of the current node.

We will use the prev pointer to update the next of the current node to be the previous node because this is what we will have when we reverse the Linked List.

We are maintaining the next pointer because once we update the next of the current node to be the previous node, we must have the access to the next of the current node to move to the next node.

Refer to the below implementation of the above approach.

```
Node reverse(Node head) {
//Initializing our three pointers.
Node prev = null;
Node current = head;
Node next = null;
/*
Running a for loop to
update the next pointer
of all the nodes.
*/
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
/*
Last node is the head
of the newly reversed
Linked List
*/
node = prev;
return node;
}
```

**Time Complexity:** The time complexity for the above approach is O(N) (where ‘N’ is the number of nodes in the Linked List) because we are just iterating the Linked List once.

**Space Complexity:** The space complexity of the above approach is O(1) because we are using the constant auxiliary space.