Question

PYTHON : Describe a fast recursive algorithm for reversing a singly linked list

PYTHON : Describe a fast recursive algorithm for reversing a singly linked list

Homework Answers

Answer #1

ANSWER:

Python program to reverse singly linked list using recursion

Algorithm :

step 1 : start

Step 2 : Create class Node

Step 3: Create constructor to initialize the node object

Step 4: Create class LinkedList

Step 5 : Define function to initialize head

Step 6: Call function reverse(a) to reverse linked list

Step 7 : Assign prev node none and current value of head

Step 8 : Define function push(a,node) to insert new node at the beginning

Step 9:   create Utility function to print the linked LinkedList

Step 10: Define Driver program to test all functions

Step 11: Stop

Program Code


class Node:
    def __init__(a, data):
        a.data = data
        a.next = None
 class LinkedList:
    def __init__(a):
        a.head = None
 
    def reverse(a, head):
 
       
        if head is None or head.next is None:
            return head      
        rest = a.reverse(head.next)   
        head.next.next = head
        head.next = None
        return rest
 
    
    def __str__(a):
        linkedListStr = ""
        temp = a.head
        while temp:
           linkedListStr = (linkedListStr + str(temp.data) + " ")
            temp = temp.next
        return linkedListStr
 
    def push(a, data):
        temp = Node(data)
        temp.next = a.head
        a.head = temp
 

linkedList = LinkedList()
linkedList.push(20)
linkedList.push(4)
linkedList.push(15)
linkedList.push(85)
 
print("Given linked list")
print(linkedList)
 
linkedList.head = linkedList.reverse(linkedList.head)
 
print("Reversed linked list")
print(linkedList)
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C++ recursive function that counts the number of nodes in a singly linked list....
Write a C++ recursive function that counts the number of nodes in a singly linked list. (a) Test your function using different singly linked lists. Include your code. (b) Write a recurrence relation that represents your algorithm. (c) Solve the recurrence relation using the iterating or recursive tree method to obtain the running time of the algorithm in Big-O notation.
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in...
Write an iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse in O(N) time. You can use as much extra space as you need. The original list pointers CAN NOT BE MODIFIED. State in big-O notation how much extra space is used by this algorithm. Write another iterative algorithm in Java-like pseudocode for printing a singly linked list in reverse using O(1) extra space. The original list pointers CAN NOT BE MODIFIED. This algorithm can have...
JAVA QUESTION: A singly linked list method that will: -A method that will find the largest...
JAVA QUESTION: A singly linked list method that will: -A method that will find the largest number in the list -A method that will find the smallest number in the list THESE ARE TWO SEPARATE BASIC METHODS
What will be the final linked-list after executing the following method on the given input singly...
What will be the final linked-list after executing the following method on the given input singly linked-list? Consider that the singly linked-list does not have a tail reference. Input: 1->2->3->4->5->6->7->8->null                                                                                                  void method(list){ if(list.head == null) return; Node slow_ref = list.head; Node fast_ref = list.head; Node prevS = null; Node prevF = null; while(fast_ref != null && fast_ref.next != null){ prevS = slow_ref; slow_ref = slow_ref.next; prevF = fast_ref; fast_ref = fast_ref.next.next; } prevS.next = slow_ref.next; prevF.next.next = slow_ref;...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after...
) IN JAVA: Write a recursive method lowestPaidEmployeeRec that is placed in the linked list after the method countHighEarners. Method returns employee with lowest salary in entire linked lists of employees. Assume the same LinkedList class as is given as in question 10 above. // PRECONDITION: Linked list is not empty. public Employee lowestPaidEmployeeRec(Node first) // first is reference to the beginning of linked list. { }
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does not have tail? Write the code. How much time does it take to Do it?
Implement Dijkstra’s Shortest Path Algorithm in C using an Adjacency List (linked list) graph.
Implement Dijkstra’s Shortest Path Algorithm in C using an Adjacency List (linked list) graph.
How do you delete the tail node of a singly linked list if the link has...
How do you delete the tail node of a singly linked list if the link has the head and does no have tail? Write the code. How much time does it take to do it? (java)
PYTHON- create recursive functions for these examples. has to be recursive with base cases and cannot...
PYTHON- create recursive functions for these examples. has to be recursive with base cases and cannot have loops! - Return sum of all the #'s inside a certain list. list should not be empty -if a list contains a certain target return True , False otherwise - if two strings contain exactly the same chars that are in the same order return True, False otherwise - if "pre" is a prefix for a string "str", return true and false otherwise
Briefly distinguish between Arrays and Linked List. Write an algorithm for each as well as develop...
Briefly distinguish between Arrays and Linked List. Write an algorithm for each as well as develop computer programs that implement each of them. Show the results of your output including your code.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT