Question
Need help on question No.5
MISU (Annoyed that this means you dont have much flexibility about how to implement this function? I apologizel But I dont
In python
Must use recursion
Must use recursion
Must use recursion
Thank you

LinkedList
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for your problem.

# Linked List class
class Node:  
    def __init__(self, data):  
        self.data = data  
        self.next = None
# recursive function to delete every other node
def accordion(old_head):
    # checks if head is none, then returns none
    if old_head == None:
        return None
    # we have to return this new_head which is the next element of the old_head
    new_head = old_head.next
    # this is a recursive call to the function
    if new_head != None and new_head.next != None:
        new_head.next = accordion(new_head.next)
    # return new_head
    return new_head
# utility functions insert and print just to demonstrate the "accordian" function
def insert_LL(head, data):
    node = Node(data)
    node.next = head
    return node
def print_LL(head):
    while head != None:
        print(head.data, end = " ")
        head = head.next
    print()
# main function for testing
if __name__ == '__main__':
    # making the LL
    head = None
    head = insert_LL(head, 1024)    
    head = insert_LL(head, "qwerty")    
    head = insert_LL(head, 13)    
    head = insert_LL(head, "asfd")    
    head = insert_LL(head, -1)    
    head = insert_LL(head, 20)    
    head = insert_LL(head, 10)    
    # printing old ll
    print("Old Linked List:", end = "\t")
    print_LL(head)
    # creating new LL and printing it
    new_head = accordion(head)
    print("New Linked List:", end = "\t")
    print_LL(new_head)


Here is the screenshot of the code if the indentation is not clear.

1 2 3 4 5 = 6 7 8 9 == 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

Here is the output of the code.

old Linked List: New Linked List: 10 20 -1 asfd 13 qwerty 1024 20 asfd qwerty

Hope this helps. Please rate the answer if you like it.
Do leave a comment. Any suggestion/query is much appreciated.

Add a comment
Know the answer?
Add Answer to:
Need help on question No.5 In python Must use recursion Must use recursion Must use recursion...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • C++ 1. Please use attached script for your reference to create the node structure and a...

    C++ 1. Please use attached script for your reference to create the node structure and a linked list class, say LinkedList, that has the following basic member methods:     constructor, destructor//IMPORTANT, display(), add_node(). 2. Please implement the following additional member methods:     Please feel free to change T with any data type you'd like to use for your node stricture's data type. -- addFirst(T data) // Adds an node with data at the beginning of the list -- pop() //...

  • RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or...

    RE-POSTED - Computer Science staff, I need this question answered. It will determine a pass or a fail. Its very imortant that this and the other questions posted are answered 100% Thank you. 13 - Template C++ Advance Please Only answer assignment if your code is 100%. When pasteing your code use text so I may copy and paste into visual studio. The code and questions must be answered 100% correct and works. Thank you. Programming Assignment Convert the int...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up...

    Please help me on all the questions !!!!!!!! Really need help! Will give a thumb up for helping. True/False (13) Chapter 14 - A List Implementation that Links Data Adding a node to an empty chain is the same as adding a node to the beginning of a chain. Adding a node at the end of a chain of n nodes is the same as adding a node at position n. You need a temporary variable to reference nodes as...

  • Need some help creating a bubble sort for my linked list in C. Here are the...

    Need some help creating a bubble sort for my linked list in C. Here are the structs used in my code: struct simulation {    void *list;    int et; /* in seconds */ }; struct plane {    double x, y, altitude;    char callsign[15];    struct simulation *sim; }; Here is the struct used in my linked list: struct Node {    void *data;    struct Node *next; }; Here is my insert function: //ComparisonFunction and FILE not...

  • I need this in C programing And please give me complete code with screenshot of your...

    I need this in C programing And please give me complete code with screenshot of your output. onded reverze. e le defnes α node stricture identical to the list nodes for the cycle detection probem in Lab4 3. Cycle le removal (20 points). Implemeat the function break.cycleO in The prototype of the fnction s The function receives as its sole argment a poister to the the link that closwes the cycle, list. If the list contains a cycle the function...

  • Java/LinkedList Need help with a few of the TODO parts, more info below in comments in...

    Java/LinkedList Need help with a few of the TODO parts, more info below in comments in bold. Thanks, package lab4; import java.util.IdentityHashMap; public class IntNode implements Cloneable {    private int data;    private IntNode next;       public IntNode(int d, IntNode n) {        data = d;        next = n;    }       public IntNode getNext() {        return next;    }          /// Override methods from Object       @Override   ...

  • ICS Quiz need help! open the Node.java file and look at the main() method driver/test method....

    ICS Quiz need help! open the Node.java file and look at the main() method driver/test method. Towards the end of the method, 5 nodes are instantiated and linked. These are what are printed out in the loop. Your task is to DRAW the structure of those 5 linked nodes using the method from the slides (list of Coins). String objects, Nodes, and pointer links must be shown. You may use software or draw on paper. Upload your drawing or a...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT