SlideShare a Scribd company logo
1 of 4
Download to read offline
Python Coding Challenges Python Link List Challenges
Reverse a Linked List In Place in Python
written by Kal Bartal February 5, 2023
Click to watch video tutorial on Reverse a Linked List In Place in Python Video Tutorial on
YouTube
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. You can 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 Python programming fundamentals.
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 linked nodes. 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
Python programming fundamentals. Manipulating the pointers of the linked list is only
sometimes 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.

More Related Content

Similar to Reverse a Linked List In Place in Python

Similar to Reverse a Linked List In Place in Python (20)

circularlinklist-190205164051.pptx
circularlinklist-190205164051.pptxcircularlinklist-190205164051.pptx
circularlinklist-190205164051.pptx
 
linked list using c
linked list using clinked list using c
linked list using c
 
Lec3-Linked list.pptx
Lec3-Linked list.pptxLec3-Linked list.pptx
Lec3-Linked list.pptx
 
data structures and applications power p
data structures and applications power pdata structures and applications power p
data structures and applications power p
 
Linkedlists
LinkedlistsLinkedlists
Linkedlists
 
Linked List.pptx
Linked List.pptxLinked List.pptx
Linked List.pptx
 
Linked list.pptx
Linked list.pptxLinked list.pptx
Linked list.pptx
 
Data Structure
Data StructureData Structure
Data Structure
 
Sy ds -I
Sy ds -ISy ds -I
Sy ds -I
 
Sy ds -I
Sy ds -ISy ds -I
Sy ds -I
 
Sy ds i
Sy ds iSy ds i
Sy ds i
 
Linked list
Linked listLinked list
Linked list
 
Linked list (1).pptx
Linked list (1).pptxLinked list (1).pptx
Linked list (1).pptx
 
Master of Computer Application (MCA) – Semester 4 MC0080
Master of Computer Application (MCA) – Semester 4  MC0080Master of Computer Application (MCA) – Semester 4  MC0080
Master of Computer Application (MCA) – Semester 4 MC0080
 
ds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdfds-lecture-4-171012041008 (1).pdf
ds-lecture-4-171012041008 (1).pdf
 
Doubly linklist
Doubly linklistDoubly linklist
Doubly linklist
 
computer notes - Linked list
computer notes - Linked listcomputer notes - Linked list
computer notes - Linked list
 
Top 10 LinkedList Interview Questions 2023 [MAANG]
Top 10 LinkedList Interview Questions 2023 [MAANG]Top 10 LinkedList Interview Questions 2023 [MAANG]
Top 10 LinkedList Interview Questions 2023 [MAANG]
 
unit 2- PPT.pdf
unit 2- PPT.pdfunit 2- PPT.pdf
unit 2- PPT.pdf
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Recently uploaded (20)

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Reverse a Linked List In Place in Python

  • 1. Python Coding Challenges Python Link List Challenges Reverse a Linked List In Place in Python written by Kal Bartal February 5, 2023 Click to watch video tutorial on Reverse a Linked List In Place in Python Video Tutorial on YouTube 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. You can 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 Python programming fundamentals. 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.
  • 2. 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 linked nodes. 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
  • 3. 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
  • 4. 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 Python programming fundamentals. Manipulating the pointers of the linked list is only sometimes 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.