# Reverse A Linked List In Python

Rate this post

## #Approach 1: Iterative Approach to Reverse Linked List

```# Linked List Node
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Solution:
def __init__(self):

# Returns the linked list in display format
def __str__(self):
while temp:
str(temp.data) + " ")
temp = temp.next

# Pushes new data to the head of the list
def push(self, data):
temp = Node(data)

previous_node = None
while current_node:
next_node = current_node.next
current_node.next = previous_node
previous_node = current_node
current_node = next_node

# Driver code

Test Cases:

```# Example 1

Output:
1 2 3 4 5
5 4 3 2 1

# Example 2

Output:
1 2
2 1

# Example 3

Output:
None
None ```

## #Approach 2: Recursive Approach to Reverse Linked List

```# Linked List Node
class Node:
def __init__(self, data):
self.data = data
self.next = None

class Solution:
def __init__(self):

# Returns the linked list in display format
def __str__(self):
while temp:
str(temp.data) + " ")
temp = temp.next

# Pushes new data to the head of the list
def push(self, data):
temp = Node(data)

def reverse_list(self, node, prev=None):
if not node:
return prev
n = node.next
node.next = prev
return self.reverse_list(n, node)

# Driver code

Test Cases:

```# Example 1

Output:
1 2 3 4 5
5 4 3 2 1

# Example 2

Output: