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.
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for...
Please show the java code and pseudocode as well Describe and implement a recursive algorithm for reversing a singly linked list L, so that the ordering of the nodes becomes opposite of what it was before. What is the running time of your algorithm on a list of n values? Provide both the growth function and BigOh complexity. Create a driver class to test your implementation.
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 Implement a θ(n) time non-recursive program that reverses a singly linked list L of n...
JAVA Implement a θ(n) time non-recursive program that reverses a singly linked list L of n elements. The program gets the head of the list (L.head) as input and cannot use any storage other than the given list. Don't not use recursive~~~~~~~ thanks
Implementing Polynomials using Singly Linked List in C++
Implementing Polynomials using Singly Linked List in C++
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
Implement a function that reverses a singly linked list in one pass of the list. The...
Implement a function that reverses a singly linked list in one pass of the list. The function accepts a pointer to the beginning of the list and returns a pointer to the beginning of the reversed list. The function must not create any new nodes. Be sure to test your function! You may start with code that you have written for other labs. This is a common interview question!
Merge two ordered singly linked lists of integers into one ordered list in C++
Merge two ordered singly linked lists of integers into one ordered list in C++
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. { }
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT