# Reverse a Linked List In Place in Python

## The Challenge

Given a singly-linked list, reverse the order of the list in place. You cannot use any additional data structure such as arrays, linked lists, etc. and you cannot create any new nodes.

## Understanding the problem

Here, we have to reverse the order of the elements of a singly-linked list in place, so you can’t use an extra data structure or create new nodes. It’ll require the nodes’ values and pointers to the next nodes in the list. What you can do is use the pointers to travel through the list, rearranging the order of the nodes as you go. Doing so will create a new list with elements in reversed order so that in the end, you’ll have the same list but with elements in the opposite order.

## Solving this problem

This problem is solvable if you understand linked lists and the fundamentals of Python programming. Don’t forget that you need to be able to manipulate the pointers, so you arrange the nodes in the right order. And don’t forget your loops, because you’ll also need those to go through all the nodes and do the needed changes.

## What is a linked list?

• Linked lists are data structures that consist of nodes linked together
• Each node contains a value and pointers to the next node in the list
• Train analogy, with each value as a passenger and pointer to the next passenger

Linked lists are data structures composed of a sequence of nodes that are linked together. Each node contains a value and a pointer to the next node in the list. Think of them like a train; each node’s value is a passenger on the train, and the pointer points to the next passenger.

An example of a linked list would be 3->2->1, which is made up of three nodes, each with a value and a pointer to the next node in the list. In this example, the first node has value of 3 and points to the second node. The second node has value 2 and points to the third node, and the third node has value 1 and does not point to any other node.

Examples:

• 3->2->1
• Node 1: Value = 3, Pointer = Next Node
• Node 2: Value = 2, Pointer = Next Node
• Node 3: Value = 1, Pointer = None

## Manipulating Pointers in Linked Lists

• Go through the linked list and make changes to the pointers
• Set the nodes’ pointers appropriately
• Set the list’s elements in reversed order

To manipulate the pointers of the linked list, you need to go through the list and make changes to the pointers. For example, if you have list 3->2->1, you would set the pointer of the first node to point to the third node instead of the second node, so it would become 3->1->2. Then, you would set the pointer of the second node to point to the first node, and so on. After you have made the necessary changes, you should have the same list, but with the elements in reversed order.

Examples:

• List 3->2->1 would become 3->1->2
• List with elements in reversed order

## Reversing a List with a Loop

• Create a variable to point to the first node
• Update the variable with each iteration of the loop
• Make changes to pointers based on current node’s position

To use a loop to iterate through the nodes and make the necessary changes, you need to have a variable that points to the first node of the list. This variable needs to be updated with each iteration of the loop.

For example, you can create a variable called “current” which initially points to the first node. Then, on each iteration of the loop, you can set “current” to point to the next node. At the same time, you can make changes to the pointers based on the position of the current node. Once the loop is done, you’ll find the list has been reversed – pretty neat!

## Reversing a Linked List In Place in Python

The following code solves this problem using an iterative approach. We start by creating a variable called “current” which points to the first node of the list, and two other variables called “next” and “previous” which are initially set to None. Then, we can use a loop to iterate through the nodes and make the necessary changes.

On each iteration of the loop, we first save the pointer of the current node in the variable “next”, point the pointer of the current node to the previous node (stored in the variable “previous”) and finally, update the variables “current” and “previous” to point to the next node and the current node respectively. After the loop is done, voilà! The elements in your list will be reversed.

``````def reverseLinkedList(head):
# Initialize variables
current = head  # points to the first node
previous = None  # initial set to None
next = None  # initial set to None

# Iterate through list and make changes
while current != None:
# Save the pointer of the current node
next = current.next

# Point the pointer of the current node to previous node
current.next = previous

# Update variables
previous = current
current = next

# List is reversed

return previous``````

## Time Complexity of Code

• Time complexity of code is O(n), as it iterates through the list once
• Number of operations is proportional to number of nodes in the list
• No extra comparisons or operations, so complexity remains linear

The time complexity of this code is O(n), because it iterates through the list once and does not use any additional data structure, so the number of operations is proportional to the number of nodes in the list. Also, it does not perform any extra comparisons or other operations on each iteration, so the time complexity remains linear.

## Space Complexity of Code

• O(1), or constant time
• The space required is independent of the size of the input
• Initializing a few variables is efficient in terms of space

The space complexity of this code is O(1), or constant time. This is because the space required to perform the operations in the reverseLinkedList() function is independent of the size of the input. We only need to initialize a few variables in order to perform the reversal, and these variables will always take up the same amount of space.  Overall, this is a very efficient algorithm in terms of space complexity.

## Final Thoughts

• Get comfortable with manipulating pointers of a linked list
• Understand the time and space complexities of a problem
• Become familiar with the fundamentals of Python programming

This is a fairly straightforward challenge, but it requires a good understanding of linked lists and the fundamentals of Python programming. Manipulating the pointers of the linked list is not always intuitive, but it is important to get comfortable with this skill, as it is useful for many other problems that involve linked lists. Understanding what the time and space complexity of a problem is also very important, as it helps you decide if a particular solution is the most efficient one.

0 comment