diff --git a/reverselinklist.py b/reverselinklist.py new file mode 100644 index 000000000000..2f71ead08873 --- /dev/null +++ b/reverselinklist.py @@ -0,0 +1,61 @@ +# Python program to reverse a linked list +# Time Complexity : O(n) +# Space Complexity : O(n) + +# Node class +class Node: + + # Constructor to initialize the node object + def __init__(self, data): + self.data = data + self.next = None + + +class LinkedList: + + # Function to initialize head + def __init__(self): + self.head = None + + # Function to reverse the linked list + def reverse(self)->None: + prev = None + current = self.head + while(current is not None): + next = current.next + current.next = prev + prev = current + current = next + self.head = prev + + # Function to insert a new node at the beginning + def push(self, new_data)->None: + new_node = Node(new_data) + new_node.next = self.head + self.head = new_node + + # Utility function to print the LinkedList + def printList(self)->None: + temp = self.head + while(temp): + print (temp.data,end=" ") + temp = temp.next + + +# Driver program to test above functions +def main()->None: + llist = LinkedList() + llist.push(20) + llist.push(4) + llist.push(15) + llist.push(85) + + print ("Given Linked List") + llist.printList() + llist.reverse() + print ("\nReversed Linked List") + llist.printList() + +if __name__ == "__main__": + main() +