minhaj-313 / Basic-Python-Programs

Basic Python Programs
9 stars 8 forks source link

Reverse a list using recursion. #66

Open minhaj-313 opened 1 year ago

GolePrateek commented 1 year ago
def reverseList(head):
        # base case, returning the last element in the list
        if head.next==None:
            return head
        # store the last node found recursively, to be returned
        last = reverseList(head.next)
        # set the next of curr node to curr node
        head.next.next = head
        # this will be changed recursively from None to the previous node
        # except for the last(new)/first(old) node
        head.next = None
        return last
minhaj-313 commented 1 year ago

@GolePrateek Do you want to add a solution for this program in this report?