I am currently learning linked lists in Python and solving such a task
Given a linked list's head, reverse it and return the head of the reversed list.
class Node:
def __init__(self, link=None, val=None):
self.val = val
self.next = link
def reverseLL(head):
reversed_head = Node()
### MY CODE
reversed_head.val = head.next.val
reversed_head.next = head
###
return reversed_head
node2 = Node(None, 5)
node1 = Node(node2, 2)
# check that your code works correctly on provided example
assert reverseLL(node1) == Node(node1, 5), 'Wrong answer'
When I'm trying the function locally, I just get the wrong answer. But when I run the code via some online grader check, it outputs:
RuntimeErrorElement(RuntimeError,Error on line 12:
reversed_head.val = head.next.val
AttributeError: 'NoneType' object has no attribute 'next'
As I understand in this task I just need to assign the head's value for reversed_head and the same for link. What am I doing wrong?