how to copy a linked list into another in a method python
Question:
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
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
Answers:
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
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
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
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