diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index b9a0aa68..7efde748 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -11,6 +11,7 @@ 1. [Doubly](linked_lists/doubly.py) 2. [Singly](linked_lists/singly.py) +3. [Reverse List](linked_lists/reverse-linkedlist.py) ## Multiplication diff --git a/algorithms/Python/linked_lists/reverse-linkedlist.py b/algorithms/Python/linked_lists/reverse-linkedlist.py new file mode 100644 index 00000000..0b3a018e --- /dev/null +++ b/algorithms/Python/linked_lists/reverse-linkedlist.py @@ -0,0 +1,51 @@ +# reverse a given linkedlist + +class Node: + # Constructor to initialize the node object + def __init__(self, data=0, next=None): + self.data = data + self.next = next + + +class LinkedList: + # Constructor to initialize head + def __init__(self) -> None: + self.head = None + + # Function to insert a new node at the beginning + def push(self, data): + node = Node(data) + node.next = self.head + self.head = node + + # Function to print the list from beginning + def printList(self): + temp = self.head + while temp: + print(temp.data, end=" ") + temp = temp.next + + # Function to reverse the list + def reverse(self): + dummy = None + cur = self.head + while cur: + nextTemp = cur.next + cur.next = dummy + dummy = cur + cur = nextTemp + self.head = dummy + + +if __name__ == '__main__': + ll = LinkedList() + ll.push(1) + ll.push(2) + ll.push(3) + ll.push(4) + ll.push(5) + print("Before Reverse") + ll.printList() + ll.reverse() # reversing + print("\nAfter Reversing") + ll.printList()