Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to copy a linked list into another in a method python

I’m new to learning linked list and one thing that I’m trying is to copy nodes of one linked list into another. I understand how to iterate through the linked list, but I don’t understand why its only coping one of the nodes into the new linked list and removing the following nodes.

what ive tried:

class Node:
    def __init__(self, value):
        self._value = value
        self._next = None

class LinkedList:
    def __init__(self):
        self._head = None
    def add(self, node):
        node._next = self._head
    self._head = node

    def copy(self):
        current = self._head
        new_list = Linkedlist()
        while current != None:
            new_list.add(current)
            current = current._next
        print(new_list)

  def main():
    l = Linkedlist()
    l.add(Node(1))
    l.add(Node(2))
    l.add(Node(3))
    l.copy()

so for a linked list of 3 2 1 instead of printing the copy as 3 2 1 it changes the original list to 3 and the new list also to 3

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the reason im printing it is so i can see why its doing this ive tried to use return and print it there but its till the same out come

>Solution :

The issue in your code is with the add() method of the LinkedList class.
The add() method should be responsible for adding a new node to the beginning of the linked list, but your implementation is not doing that correctly.

In your add() method, the assignment self._head = node is outside the method’s block and is not properly indented.
This causes the method to only set the _head attribute once, to the node that is passed to the first call to add().
All subsequent calls to add() do not update the _head attribute and do not add the new node to the beginning of the linked list.

To fix this issue, you need to indent the assignment statement so that it is part of the add() method’s
block. Here is the corrected implementation:

class Node:
    def __init__(self, value):
        self._value = value
        self._next = None

class LinkedList:
    def __init__(self):
        self._head = None

    def add(self, node):
        node._next = self._head
        self._head = node

    def copy(self):
        current = self._head
        new_list = LinkedList()
        while current != None:
            new_list.add(Node(current._value))
            current = current._next
        return new_list
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading