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
  • 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...

  • 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() //...

  • I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement...

    I need help Writing a Python code!!! Implement an ordered list using doubly linked list Implement the following operations for an ordered list of integers ordered in ascending order using a doubly linked list. The “head” of the list be where the “smallest items are and let “tail” be where the largest items are. You may use whatever mechanism you like to keep track of the head and tail of the list. E.g. references or sentinel nodes. • OrderedList ()...

  • 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...

  • In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains,...

    In Python 3 Write a LinkedList class that has recursive implementations of the display, remove, contains, insert, and normal_list methods. You may use default arguments and/or helper functions. The file must be named: LinkedList.py Here is what I have for my code so far. The methods I need the recursive implementations for will be bolded: class Node: """ Represents a node in a linked list (parent class) """ def __init__(self, data): self.data = data self.next = None class LinkedList: """...

  • 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   ...

  • In this assignment you are to utilize the Node data structure provided on Blackboard. In this...

    In this assignment you are to utilize the Node data structure provided on Blackboard. In this assignmet you are to write a main program that implements two methods and a main method as their driver. So, only main and two methods with it. Note: You may not use any of the LinkedList class provided on Blackboard, you may use the methods in it as a guide (you should actually look at them) but you cannot use any of the methods...

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
ADVERTISEMENT