# Python Challenge - Reverse a Singly-Linked List

This community-built FAQ covers the “Reverse a Singly-Linked List” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the Python challenge Reverse a Singly-Linked List

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like () to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

``````from LinkedList import *

def __iter__(self):
node = Node(self).data
while node:
yield node.data
node = node.next

revList = None
revList = Node(i, revList)
return revList

print("Original")
print("Reversed")
print("Original Unchanged")
``````
from LinkedList import * def reverse_linked_list(linked_list): first_node = linked_list second_node = None while first_node != None: new_node = Node(first_node.data, second_node) second_node = new_node first_node = first_node.next return second_node demo_list = make_linked_list([4,8,15]) reverse = reverse_linked_list(demo_list)

Build the reversed list by iterating through the given linked list from first to last and prepending every node to the reversed list, pointing each node’s `next` pointer to the previously added node.

Has anyone done this challenge using recursion? (I did not.)

I added in checking if each node had a `next` attribute, otherwise I kept getting an error.
My solution is longer than the ones in the other posts.

My solution to the challenge
``````def reverse_linked_list(linked_list):
return None
if not hasattr(current_node, "next"):
return Node(current_node.data)
next_node = current_node.next
reversed_node_list = []
reversed_node = Node(current_node.data)
reversed_node_list.append(reversed_node)

while next_node is not None:
reversed_node = Node(next_node.data, reversed_node)
reversed_node_list.insert(0, reversed_node)
current_node = next_node
if not hasattr(current_node, "next"):
break
next_node = current_node.next

return reversed_node
``````

Here’s proof that it works:

I had to put in a Node class for the code to run.

Similar to above solution

``````from LinkedList import *

return None
return node
``````
class Node: def __init__(self, data, next_node=None): self.data = data self.next = next_node def print_linked_list(self): print(self.data) current_node = self.next while current_node: print(current_node.data) current_node = current_node.next def make_linked_list(array): last_node = None for value in reversed(array): current_node = Node(value, last_node) last_node = current_node return current_node def reverse_linked_list(linked_list): current_node = linked_list new_node = None while current_node: new_node = Node(current_node.data, new_node) current_node = current_node.next return new_node print("Original") demo_list = make_linked_list([4,8,15]) demo_list.print_linked_list() print("Reversed") reverse = reverse_linked_list(demo_list) reverse.print_linked_list() print("Original Unchanged") demo_list.print_linked_list()
``````
data_list = []
while (node.next) != None:
data_list.append(node.data)
node = node.next
data_list.append(node.data)
reverse_list = data_list[::-1]
return result

print("Original")
print("Reversed")
print("Original Unchanged")

``````

Have a smiliar result.

from copy import deepcopy

prev_node = None
curr_node = copy_list
next_node = None

while curr_node != None:
next_node = curr_node.next
curr_node.next = prev_node
prev_node = curr_node
curr_node = next_node

return prev_node

print(“Original”)