Reverse linked list
I. Reverse Linked List
A fairly common "beginning" question asked at large companies such as Google, Facebook, Uber, . is to reverse a singly linked list. It can be said that this is a classic question on the topic of Linked List. In this article, I will give 2 ways to solve the problem and analyze its space and time complexity. I will assume that you already know about the linked list data structure when reading this article. If not, you can read the document "Basic linked list" by Stanford University, USA here.
II. Topic
Write a function that takes as input the head of a Singly Linked List, reverses it and returns the head of the reverse linked list.
This is the implementation of Linked List that I will use in the article:
Sample Input:
head = 0 -> 1 -> 2 -> 3 -> 4 -> 5 # the head node with value 0
Sample output:
head = 5 -> 4 -> 3 -> 2 -> 1 -> 0
There are many solutions to this problem, but in this article I will only give two that I think are the most typical.
1. Solution 1: use list as intermediary
Because a singly linked list can only traverse in one direction (forward) while a list can reverse itself and traverse in both directions (forward, reversed) easily. So the solution that many people have when encountering this problem is to convert the linked list to a list, reverse that list, and then rebuild the linked list using the reversed list. Below is a description of my solution:
Illustration of the solution using list
And here is my solution:
1.1 Space and time complexity analysis
Note: n is the number of nodes of the input linked list
Space complexity is O(n):
- The space complexity of constructing a list of n elements from a linked list is O(n)
- The space complexity of constructing a linked list from a list of n elements is O(n)
- Therefore, the space complexity of this solution is O(n)
Time complexity is O(n):
- The time complexity of constructing a list of n elements from a linked list is O(n)
- The time complexity of constructing a linked list from a list of n elements is O(n)
- Therefore, the time complexity of this solution is O(n)
2. Solution 2: swap nodes in-place
The solution will be illustrated by the following doodle:
Illustration of how to solve swapping nodes
The explanation of the solution is as follows:
- We save the positions of 3 nodes in the linked list (previous, current and following)
- Attach the next node of current to previous
- Then we move the position of 3 nodes (current → previous, following → current, current.next -> following) and then attach the next node of current to previous
- Just repeat the above steps until we reach the end of the linked list and we have completed reversing a linked list.
And here is my solution:
class LinkedList: def __init__(self, value):
self.value = value
self.next = None
def reverseLinkedList(head):
previous_node, current_node = None, head
while current_node:
following_node = current_node.next
current_node.next = previous_node
previous_node, current_node = current_node, following_node
return previous_node
2.2 Space and time complexity analysis
Note: n is the number of nodes of the input linked list
Space complexity is O(1)
- With this solution, we only swap nodes in the input linked list - no additional space is generated.
- Therefore, the total space complexity is O(1)
Time complexity is O(n):
- Time complexity to iterate through nodes in linked list is O(n)
- Therefore, the total time complexity is O(n)
III. Conclusion
The above two solutions are not all the ways to reverse a linked list but they are two typical ways for this classic problem. Hopefully this blog post can help you in some way. If you find the blog post interesting, please share it so that more people know about it and look forward to more of my blog posts on the topic of Data Structure & Algorithm on TipsMake's tech blog!
You should read it
- Linked list data structure (Linked List)
- Data Link List structure (Circular Linked List)
- Shift linked list problem
- Microsoft adds a page to manage devices sharing the same user account on Windows 11
- The To-do List is good, the Done List is very good, but the Do-Not-Do List is much better
- How to create a drop list in Excel 2016
- Work with lists in PowerPoint 2016
- How to Simplify Your To-Do List
May be interested
- How to make a video rewind, make the clip reverse on the phonereverse video, reverse video will help you get very interesting moments to share with friends and relatives
- How to use Nginx as a reverse proxyunlike apache, nginx is the most popular web server available. in addition to being a web server, it can also be used as a load proxy or reverse proxy.
- Malware reconciliation design (part 1)for many people today, reverse engineering is a rather strange mechanism. they don't even know what it is and how to use it. why can reverse use of architecture of an executable? the skills needed to perform
- How to reverse video online for freewhen reversing the video will create a very unique, interesting effect, in addition to other video effects.
- How to reverse the color displayed on the Macscreen color reversal is a popular accessibility feature available on almost every operating system today. macos also has an option to do this on a mac.
- Instructions to reverse video on Capcutcapcut application is currently one of the most widely used video editing and video editing applications. the application provides you with many different editing options, from basic to advanced so that you can get the video art you want.
- Reverse Image Search on iPhonereverse image search reverses the normal image search process, instead of searching with words or phrases, users will use images to search. this article will show you how to search backwards with images on iphone.
- How to fix the computer screen error is reversedreverse rotation screen is a common mistake that many people make when using windows 7/8 / 8.1 / 10 computers, because while pressing the wrong key on certain operations. or install software, games, ... to change windows settings. so what to do to bring the computer back to a standard angle?
- WhatsApp is about to launch Reverse Image Search featurereverse image search tools are really useful for discovering the original source of a photo, but saving a photo from one app and re-uploading it to another can be a pain.
- Advice on how to use the camera to reverse the car for new driversdetailed instructions on how to use the camera to reverse cars. what are the notes when using this device during parking, advancing, and docking?