diff --git a/ReverseLinkList.py b/ReverseLinkList.py new file mode 100644 index 0000000..a96aac2 --- /dev/null +++ b/ReverseLinkList.py @@ -0,0 +1,98 @@ + +from sys import stdin, setrecursionlimit +setrecursionlimit(10 ** 6) + +#Following is the Node class already written for the Linked List +class Node : + def __init__(self, data) : + self.data = data + self.next = None + + + +#Iterative Approach +def Reverse(head): + curr = head + prev = None + while (curr is not None): + next = curr.next + curr.next = prev + prev = curr + curr = next + return prev #As our head is now prev + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +#Taking Input Using Fast I/O +def takeInput() : + head = None + tail = None + + datas = list(map(int, stdin.readline().rstrip().split(" "))) + + i = 0 + while (i < len(datas)) and (datas[i] != -1) : + data = datas[i] + newNode = Node(data) + + if head is None: + head = newNode + tail = newNode + + else : + tail.next = newNode + tail = newNode + + i += 1 + + return head + + +#to print the linked list +def printLinkedList(head) : + + while head is not None : + print(head.data, end = " ") + head = head.next + + print() + + +#main +t = int(stdin.readline().rstrip()) + +while t > 0 : + + head = takeInput() + head = Reverse(head) + printLinkedList(head) + print() + + t -= 1