I am using recursion to reverse the linked list but not getting the result. Whenever I am using the following code for the input [1,2,3,4,5,6,-1]
, it shows me the result [1->None]
. I am not getting where I am making the mistake. Please help me.
class Node:
def __init__(self, data):
self.data = data
self.next = None
def takeInput():
inputList = [int(ele) for ele in input().split()]
head = None
for i in inputList:
if i == -1:
break
newNode = Node(i)
if head is None:
head = newNode
tail = newNode
else:
tail.next = newNode
tail = newNode
return head
def printLL(head):
while head is not None:
print(str(head.data) + "->", end="")
head = head.next
print("None")
return
def reverseLL(head):
if head is None or head.next is None:
return head
rest = reverseLL(head.next)
head.next.next = head
head.next = None
return rest
head = takeInput()
printLL(head)
reverseLL(head)
printLL(head)
[1,2,3,4,5,6,-1]
"1 2 3 4 5 6 -1"
takeInput
function but I am still not sure what the OP is looking for