Question
in phyton please.
b. Given a singly linked list, Your task is to remove every K-th node of the linked list. Assume that K is always less than o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

"""

Program to remove kth node.

Python version:3.6.1

"""

class Node:

    """

    A node in a linked list

    """

    def __init__(self, value):

        self.value = value

        self.next = None

def print_list(head:Node):

    list_str = ""

    if head is None:

        print("Empty List")

    while head is not None:

        list_str = f"{list_str}{head.value}"

        if head.next is not None:

            list_str = f"{list_str}->"

        

        head = head.next

    

    print(list_str)

def remove_kth_nodes(head:Node, k:int):

    """

    modifies the head node to remove every kth node, and returns the new list

    """

    # A check to verify if k less than the length of the linked list

    # is not needed since it has been clearly mentioned that k is always less

    # k == 0 is invalid

    if k == 0:

        raise Exception("k cannot be 0")

    # k == 1 means first node

    if k == 1:

        return None

    # find every k - 1 th node and remove it

    current = head

    

    # Start from first node, therefore i = 1

    i = 1

    while current is not None:

        if i == k-1:

            # remove the next node, if it is not none

            if current.next is not None:

                current.next = current.next.next

            current = current.next

            i = 1

        else:

            # skip the next node

            current = current.next

            i += 1

    return head

    

if __name__ == "__main__":

    n = Node(1)

    n.next = Node(2)

    n.next.next = Node(3)

    n.next.next.next = Node(4)  

    n.next.next.next.next = Node(5)

    n.next.next.next.next.next = Node(6)

    n.next.next.next.next.next.next = Node(7)

    n.next.next.next.next.next.next.next = Node(8)

    print_list(n)

    n = remove_kth_nodes(n, 3)

    print_list(n)

    

Add a comment
Know the answer?
Add Answer to:
in phyton please. b. Given a singly linked list, Your task is to remove every K-th...
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
  • java singly linked list write a routine, which will travel through the list second and third...

    java singly linked list write a routine, which will travel through the list second and third list by taking every 2nd node from the first list and 3rd node Given a pointer to creating from the first list and putting them into the two newly created lists. (For example: If the original list had 12 nodes it should remain with nodes 1, 4, 7, and 10. The 2nd list would be made up of nodes 2, 5, 8, and 11...

  • how to implement a linked list object using only singly linked list toolkit. Then implement a...

    how to implement a linked list object using only singly linked list toolkit. Then implement a FREQUENCY function to count the ovcurrence of each element in the list. task#1: Add = operator to node: implement the assignment operator for the node such that setting a node = overwrites the value in the node with the value. task#2:Linked List class implement the methods of linked list. insert search and locate remove node* operator [] task#3: Implement the Frequency

  • 1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head...

    1)Given a singly linked list contains four nodes and simply show as 4->3->2->1, where the head reference refers to the first node contains an Integer with value 4. If Node curr = head; curr= curr.next; are applied to this list, what is the number you can find in the first node? 2) Given a singly linked list contains 6 nodes, which is simply shown as 1->2->3->4->5->6. Assume head refers to the first node (contains 1) on the list. How many...

  • c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store...

    c++ Computational Complexity Create a singly linked list for storing positive integers. Each node will store one integer. For example, 12->3->5->777-111 is such a list. There are five nodes in this list. 12 is the head node and 111 is the tail node. (111 points to NULL.) Your linked list starts empty. It should support the following three operations: Add x to tail: Create a new node whose data field contains x. Append this node at the end of the...

  • Question 2 (3 points) Given the following singly linked list (a list with four nodes), what...

    Question 2 (3 points) Given the following singly linked list (a list with four nodes), what will be the value stored at the last node when the following operations are executed? head->1111-151->11011->1211 Node curr = head; while(curr next !=null) { curr.item = curritem-head. item; curr = cur next; اسرة Node newNode = new Node(8): curr.next = newNode: 23 O 12 OS O21 Question 3 (3 points) Given a recursive method as shown below, what is the return value for P(5)...

  • Answer all questions 1- in circular singly linked list, previous pointer of the first node points...

    Answer all questions 1- in circular singly linked list, previous pointer of the first node points to which node A. First node B. Itself C. null D. Last node 2- Which of the following is NOT an applications of linked lists? A. Implementation of stacks and queues B. Dynamic memory allocation C. Manipulation of polynomials D. Keeping people at home during epidemics like corona virus 3- In a circular singly linked list? A. Components are all linked together in some...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • 2. Inthe pseudocode program below, list is an initially empty Singly Linked List: The function populatelist()...

    2. Inthe pseudocode program below, list is an initially empty Singly Linked List: The function populatelist() adds the integers [8, 5, 1, 5, 2, 7] to the tail of list sequentially. What is the output of the program? Select 'No Answer' if the program results in an error. populatelist(); int sum = 0; Node n = list.head; // list.head/list.tail points to the first/last integer in list sum += n.value; sum += n.value; sum += n.next.value; n = n.next; sum +=...

  • Build a singly linked list with 10 noes. The data are "This is my first project...

    Build a singly linked list with 10 noes. The data are "This is my first project in Data structure and algorithm". First node is "This" second "is" and so on. Build insert function, insert "the " before "Data" and delete function, delete "my" before "first". Search "in" and output it location and then append "C++" and output the linked list again. In writing this program use menu which includes ”1. Create; 2. Insert; 3. Delete; 4. Append; 5. Search; 6....

  • 2 Given a pointer to a singly linked list write a routine, which will travel through...

    2 Given a pointer to a singly linked list write a routine, which will travel through t creating a second list by taking every 3nd node from the first list and putting it ir newly created list. (For example: If the original list had 12 nodes it should remai nodes 1, 2, 4, 5, 7, 8, 10 and 11 while the 2nd list would be made up of nodes 3, 6, 12. Briefly explain: A. The advantage of implementing 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
ADVERTISEMENT