Currently, I've been trying to rearrange a linked list based on my main function switch(myList,index).
def createList(plist):
linkedList = None
# goes backwards, adding each element to the beginning
# of the list.
for index in range(len(plist)-1, -1, -1):
linkedList = insertValueHead(linkedList, plist[index])
return linkedList
def insertValueHead(linkedList, value):
newnode = {}
newnode["data"] = value
#set the next pointer of this new node to the head of the list, linkedList
#newnode is now the head of the list
newnode["next"] = linkedList
return newnode
def listString(linkedList):
ptr = linkedList
str1 = ''
while ptr != None:
str1 += str(ptr['data'])
ptr = ptr['next']
if ptr != None:
str1 += "->"
str1 = str1
return str1
def switch(j, i):
head = j
currentItem = j[0] # The head again
prevItem = 1 # The item that links to tempItem
for x in range(i): # Find the item to swap
prevItem = currentItem
currentItem = currentItem['next']
currentItem = currentItem['next']
temp = currentItem['next']
currentItem['next'] = head['next']
head['next'] = prevItem['next']
prevItem['next'] = temp
def testSwitch():
#test code to ensure that switch() is working correctly.
myList = createList([10, 20, 30, 40, 50, 60])
print "The initial list", listString(myList)
myList = switch(myList, 2)
print "Switching the 1 and the 2. Resulting list is ", listString(myList)
testSwitch()
This should yield a list with swapped elements. However, when I run it this is the output:
The initial list 10->20->30->40->50->60
Switching the 1 and the 2. Resulting list is
This is then followed by the error:
currentItem = currentItem['next']
TypeError: list indices must be integers, not str
What am I doing wrong? I can't seem to figure it out...
The simply-linked list is not a very useful construct, if you need to support switch operation. On a doubly linked list, if the nodes have the pointers forward and backward, then it is very easy, but on singly linked list you need to scan the list at least once. Also, your code is so messy that no one can really debug it. Thus
use class-based lists instead having items of a Node subclass, say.
for switch operation you seriously want to have doubly linked list.
Maybe use the linux linked list convention, where the ends are a list node too
Something like
class Node(object):
prev = None
next = None
class List(object):
def __init__(self):
self.head = self
self.tail = self
self.prev = self
self.next = self
self.nil = self
def insert_head(self, node):
node.next = self.head.next
self.head.next.prev = node
node.prev = self.head
self.head.next = node
def __iter__(self):
current = self.head.next
while current != self.nil:
yield current
current = current.next
def __str__(self): # the "list_string" op
items = []
return ' -> '.join(map(str, self))
class TestNode(Node):
def __init__(self, value):
self.value = value
def __repr__(self):
return repr(self.value)
list = List()
list.insert_head(TestNode('a'))
list.insert_head(TestNode('b'))
print(list)
Your indentation isn't proper, that's why you are getting the issue.
def switch(j, i):
head = j
currentItem = j[0] # The head again
prevItem = 1 # The item that links to tempItem
for x in range(i): # Find the item to swap
prevItem = currentItem
currentItem = currentItem['next']
currentItem = currentItem['next']
temp = currentItem['next']
currentItem['next'] = head['next']
head['next'] = prevItem['next']
prevItem['next'] = temp
Give an indent of four spaces inside a function or a loop. You can't assign j[0] to current item as it's a dict.
Related
I am trying to remove the kth element from the END of a linked list. Here is my code
class LinkedList:
def __init__(self, value):
self.value = value
self.next = None
def removeKthNodeFromEnd(head, k):
if k == 0 or head is None:
return
temp = head
while temp is not None and k > 1:
temp = temp.next
k -= 1
if temp is None or k > 1:
return head
trailing = None
leading = head
while temp.next is not None:
trailing = leading
leading = leading.next
temp = temp.next
if trailing is None:
head = head.next
else:
trailing.next = leading.next
head = LinkedList(0)
head.next = LinkedList(1)
head.next.next = LinkedList(2)
head.next.next.next = LinkedList(3)
head.next.next.next.next = LinkedList(4)
head.next.next.next.next.next = LinkedList(5)
head.next.next.next.next.next.next = LinkedList(6)
head.next.next.next.next.next.next.next = LinkedList(7)
head.next.next.next.next.next.next.next.next = LinkedList(8)
head.next.next.next.next.next.next.next.next.next = LinkedList(9)
removeKthNodeFromEnd(head, 10)
while head is not None:
print(head.value)
head = head.next
But this does not work and prints out all the values in the linked list from 0 to 9. Why is this the case? If the node to delete is the head node which I check by checking trailing is None, then I update head = head.next. I am changing head to be the next node. I can return head after this update and the result of head = removeKthNodeFromEnd(head, k) will give me the desired output but why can't I do it the other way without returning anything? For instance in my solution I can get rid of any element in between the first and last node including the last and works just fine. The original head gets updated and node gets removed. But when trying to update the head node head = head.next it does not work.
One way I achieved this is by doing the following...
if trailing is None:
head.value = head.next.value
head.next = head.next.next
But why must I use the values? To me this seems the same as
if trailing is None:
head = head.next
head.next = head.next.next
but does not work
It doesn't work because head in that function is a local name. Assigning to a local variable never does anything to other variables, even not when they happen to have the same name (like the global head).
The "trick" you have by moving a linked list value with head.value = head.next.value will not work when the list only has one node.
One way to do this, is to return the (potentially modified) value of head, and expect the caller to assign this back to their own head variable.
So:
in removeKthNodeFromEnd make sure all return statements are like return head, and that you add a return head at the very end. In short, removeKthNodeFromEnd should always return head.
in the main program, change the call to head = removeKthNodeFromEnd(head, 10)
That will solve your issue.
Here are some ideas to make your code more elegant:
Create a separate class for maintaining the head of the linked list
Improve the constructor, so that it is easier to initialise a linked list
Add an iterator so it is easier to print a linked list
class Node: # renamed
def __init__(self, value, nxt=None):
self.value = value
self.next = nxt # is now an argument
class LinkedList: # added
def __init__(self, *values):
self.head = None
if values: # populate the list with values
for value in reversed(values):
self.push_front(value)
def push_front(self, value):
self.head = Node(value, self.head)
def __iter__(self):
node = self.head
while node:
yield node.value
node = node.next
def removeKthNodeFromEnd(self, k):
if k <= 0:
return
temp = self.head
for _ in range(k):
if not temp: # k is out of range
return
temp = temp.next
if temp:
trailing = self.head
while temp.next:
trailing = trailing.next
temp = temp.next
trailing.next = trailing.next.next
else:
self.head = self.head.next
lst = LinkedList(*range(10))
print(*lst)
lst.removeKthNodeFromEnd(0)
print(*lst)
And if you maintain a size attribute, you can do:
class LinkedList: # added
def __init__(self, *values):
self.head = None
self.size = 0
if values:
for value in reversed(values):
self.push_front(value)
def push_front(self, value):
self.head = Node(value, self.head)
self.size += 1
def __iter__(self):
node = self.head
while node:
yield node.value
node = node.next
def removeAt(self, k):
if 1 < k <= self.size:
trailing = self.head
for _ in range(k - 2):
trailing = trailing.next
trailing.next = trailing.next.next
elif 1 == k <= self.size:
self.head = self.head.next
def removeKthNodeFromEnd(self, k):
self.removeAt(self.size + 1 - k)
class Node:
def __init__(self, data = None, next = None):
self.data = data
self.next = next
class LinkedList:
def __init__(self, node = None):
self.head = node
self.length = 0
def InsertNode(self, data):
newNode = Node()
newNode.data = data
if self.length == 0:
self.head = newNode
else:
newNode.next = self.head
self.head = newNode
self.length += 1
def printList(self):
temp = self.head
while temp != None:
print(temp.data, end = " ")
temp = temp.next
class AddingListNumbers:
def addTwoNumbers(self, list1, list2):
if list1 == None:
return list2
if list2 == None:
return list1
len1 = len2 = 0
head = list1.head
while head != None:
len1 += 1
head = head.next
head = list2.head
while head != None:
len2 += 1
head = head.next
if len1 > len2:
shorter = list2
longer = list1
else:
shorter = list1
longer = list2
sum = None
carry = 0
while shorter != None:
value = shorter.data + longer.data + carry
carry = value / 10
value -= carry * 10
if sum == None:
sum = Node(value)
result = sum
else:
sum.next = Node(value)
sum = sum.next
shorter = shorter.next
longer = longer.next
while longer != None:
value = longer.data + carry
carry = value / 10
value -= carry * 10
sum.next = Node(value)
sum = sum.next
longer = longer.next
if carry != 0:
sum.next = Node(carry)
return result
linkedlist = LinkedList()
linkedlist.InsertNode(19)
linkedlist.InsertNode(14)
linkedlist.InsertNode(11)
linkedlist.InsertNode(9)
linkedlist.InsertNode(6)
linkedlist.InsertNode(5)
linkedlist2 = LinkedList()
linkedlist2.InsertNode(17)
linkedlist2.InsertNode(16)
linkedlist2.InsertNode(13)
linkedlist2.InsertNode(6)
linkedlist2.InsertNode(2)
linkedlist2.InsertNode(1)
linkedlist2.InsertNode(24)
linkedlist2.InsertNode(3)
linkedlist2.InsertNode(11)
list3 = LinkedList()
ResultList = AddingListNumbers()
list3.next = ResultList.addTwoNumbers(linkedlist, linkedlist2)
list3.printList()
I have created Node and LinkedList classes and then another class AddingListNumbers for adding the list number.
I am getting an error message:
value = shorter.data + longer.data + carry
AttributeError: 'LinkedList' object has no attribute 'data'
I don't understand how to debug this one. How to handle attribute errors?
Below is the image of the error message.
The issue is with this section:
if len1 > len2:
shorter = list2 # should be list2.head instead
longer = list1 # should be list1.head instead
else:
shorter = list1 # should be list1.head instead
longer = list2 # should be list2.head instead
With that said, however, this code can be optimised to traverse the lists only once. In this implementation you're first finding the shorter list and then adding while the shorter list is traversed. It can be done in single traversal as:
Repeat these steps while list1 is traversed OR list2 is traversed completely
Add data from node of list1, if it exists, or add 0
Add data from node of list2, if it exists, or add 0
Add carry to the above sum.
Calculate divmod of the sum and re-assign carry and sum.
Move pointers ahead, if next nodes are present.
The resultant list will have sum of the lists.
From list first I should do LinkedList and put elements into it (I did that). From that list I have to move all not string elements (integers) to another LinkedList and print them also.
Initialisation:
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
self.size = 0
def append(self,new_data):
new_node = Node(new_data)
if self.head is None:
self.head = new_node
return
last = self.head
while(last.next):
last = last.next
last.next = new_node
def Lista(lista):
linked = LinkedList()
for element in lista:
linked.append(element)
return linked
Function for print and function(izdvoji) to move elements to another linkedlist:
def ispis(self):
temp = self.head
while (temp):
print (temp.data)
temp = temp.next
print("------")
def izdvoji(self):
linked = LinkedList()
temp = self.head
while(temp):
if isinstance (temp.data, str):
linked.append(temp.data)
temp = temp.next //edited
else:
temp = temp.next
if __name__ == '__main__':
L = Lista([33, "asp","oop1",5,21,"python",2,"c++"])
ispis(izdvoji(L))
ispis(L)
Result must be:
asp,oop1,python,c++
33,5,21,2
Sorry for long code but I want you to see it.
Edit function:
def izdvoji(linkedLista):
linked = LinkedList()
temp = linkedLista.head
while(temp != None):
if isinstance (temp.data, str):
linked.append(temp.data)
temp = temp.next
temp = linkedLista.head
while(temp != None):
if temp == linkedLista.head and isinstance (temp.data, str):
linkedLista.head = temp.next
print(temp.data)
if temp.next != None and isinstance (temp.next.data, str):
temp.next = temp.next.next
temp = temp.next
return linked
Output:
asp,oop1, python,c++,
33, oop1, 5, 21, 2,
The print function accepts an end parameter which defaults to "\n". You can it to be ", " to print your list.
To move your nodes from one list to another you need to do two steps
Copy the node to new list
Delete the node from old list
You did not have a delete method to handle that so I had to write delete method myself.
Delete method for linked list
def delete(self, node_data):
temp = self.head
# If list is empty
if temp is None:
return
# If the head need to be removed
if temp.data == node_data:
self.head = temp.next
return
# Iterate over the list and stops when the next node is our required node
while temp is not None and temp.next.data != node_data:
temp = temp.next
# if temp is none that means we reached the end of list
if temp is None:
return
# removing the node
temp.next = temp.next.next
def ispis(linked_list):
temp = linked_list.head
while (temp):
print(temp.data, end=", ")
temp = temp.next
print("\n------")
Output:
33, asp, oop1, 5, 21, python, 2, c++,
------
As far as copying goes. You forgot to update the temp when the if condition was true. Technically you don't need else here as well. You also need to return the list after moving the elements which you are not doing.
def izdvoji(linked_list):
linked = LinkedList()
temp = linked_list.head
while temp:
if isinstance(temp.data, str):
linked.append(temp.data) # Copy node to new list
linked_list.delete(temp.data) # delete from old list
temp = temp.next
return linked
Output:
asp, oop1, python, c++,
------
33, 5, 21, 2,
------
Main Code:
if __name__ == '__main__':
L = Lista([33, "asp","oop1",5,21,"python",2,"c++"])
ispis(izdvoji(L))
ispis(L)
Note: By convention self is only used for to refer the attributes and methods of the class. izdvoji and ispis are not class methods. So using self here makes no sense.
I am currently taking an online computer science introductory course and have just learned the concept of a linked list. Though I understand the concept of linked lists, I still am unsure as how to deal with linked lists.
As such, I seek out help in solving the following problem, which will be of significant help for my understanding of linked lists:
Write a function (not in LinkedList class definition) that given a linked list, will change that linked list to filter out odd numbers. Immediately after the function returns, the linked list will only have even numbers.
I am unsure as to how to access the nodes in the list and check whether they are odd or even and remove or keep them accordingly.
I apologize if this seems like a trivial question, but I would appreciate any help that might help me learn.
The code for the linked list and node classes (as provided by the online course):
class Node:
def __init__(self, data=None, next_node=None):
self.data = data
self.next = next_node
def __str__(self):
return str(self.data)
class LinkedList:
def __init__(self):
self.length = 0
self.head = None
def print_list(self):
node = self.head
while node is not None:
print(node, end=' ')
node = node.next
print('')
def add_at_head(self, node):
node.next = self.head
self.head = node
self.length += 1
def remove_node_after(self, node):
if node.next is not None:
temp = node.next
node.next = node.next.next
temp.next = None
self.length -= 1
def remove_first_node(self):
if self.head is None:
return
temp = self.head
self.head = self.head.next
temp.next = None
self.length -= 1
def print_backward(self):
def print_nodes_backward(node):
if node.next is not None:
print_nodes_backward(node.next)
if node is not None:
print(node, end=' ')
if self.head is not None:
print_nodes_backward(self.head)
print('')
Let's say you have a bare-bones simple linked list that looks like this:
class LinkedList:
class ListNode:
def __init__(self, data):
self.data = data
self.next = None
def __init__(self):
self.head = None
def add(self, data):
if self.head is None:
self.head = LinkedList.ListNode(data)
else:
current_node = self.head
while current_node.next is not None:
current_node = current_node.next
current_node.next = LinkedList.ListNode(data)
def __str__(self):
ret = "["
current_node = self.head
while current_node is not None:
ret = ret + str(current_node.data)
if current_node.next is not None:
ret = ret + ", "
current_node = current_node.next
ret = ret + "]"
return ret
In other words, the LinkedList contains a single head, which is a ListNode. Every element in the Linked List is contained in a ListNode, and each ListNode points towards the next element in the list.
As you can see, for adding an element to the list, we either create a node at the head if the list is empty (i.e. self.head is None), or we traverse to the end of the list by continuously jumping to the .next element for each ListNode, starting from the head. We also use this paradigm for printing a string representation of our list.
So, to remove any node from the linked list, we can simply change the node that references it, so that the node we want to remove gets skipped. At which point it will disappear.
To remove all list nodes containing odd-numbered data, we might do something like this:
def remove_odds(self):
# special case: head node
# remove odd head elements by simply setting head to the next element after
while (self.head is not None) and (self.head.data % 2 == 1):
self.head = self.head.next
# regular case: the rest of the nodes
current_node = self.head
while (current_node is not None) and (current_node.next is not None):
# if the next node's data is odd, then
if current_node.next.data % 2 == 1:
# skip that node by pointing this node's .next to the next node's .next
current_node.next = current_node.next.next
# otherwise, move forwards in the list
else:
current_node = current_node.next
Proof of concept:
>>> lst = LinkedList()
>>> lst.add(2)
>>> lst.add(5)
>>> lst.add(6)
>>> lst.add(3)
>>> lst.add(7)
>>> lst.add(8)
>>> lst.add(10)
>>> lst.add(1)
>>> lst.add(4)
>>> print(lst)
[2, 5, 6, 3, 7, 8, 10, 1, 4]
>>> lst.remove_odds()
>>> print(lst)
[2, 6, 8, 10, 4]
Copied from comment: The idea is to iterate through the list head-to-tail while remembering the previous node; when you find a garbage node, apply remove_node_after to the remembered node, or move the head to the current node if we haven't had time to remember anything yet.
The code would be something like this (untested):
class LinkedList:
# ...
def delete_if(self, pred):
prev = None
curr = self.head
while curr:
if pred(curr.data):
if prev:
self.remove_node_after(prev)
else:
self.head = curr
prev = curr
curr = curr.next
llist.delete_if(lambda x: x % 2 == 1) # delete if odd
# Mahmoud AL-Mokdad
# this course on Udemy By SEfactoru right😁
# use my code 😉
def filter_even(ll):
first_node = ll.head
while (first_node is not None) and (first_node.data % 2 != 0):
ll.remove_first_node()
first_node = ll.head
node = first_node
while node is not None and node.next is not None:
if node.next.data % 2 != 0:
ll.remove_node_after(node)
else:
node = node.next
I am a bit new to python and I have seen the correct solutions to the reversing the linkedlist problem but I wanted to know why my solution does not work. In particular, reverse function stays inside the while loop for the code below because of "new_list.head.next=prev" line
class Node:
def __init__(self, value):
self.value = value
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def append(self, value):
if self.head is None:
self.head = Node(value)
return
node = self.head
while node.next:
node = node.next
node.next = Node(value)
def __iter__(self):
node = self.head
while node:
yield node.value
node = node.next
def __repr__(self):
return str([v for v in self])
def reverse(linked_list):
new_list = LinkedList()
if linked_list is None:
return new_list
node = linked_list.head
new_list.head = node
while node.next:
prev = node
node = node.next
new_list.head = node
new_list.head.next = prev
return new_list
if __name__ == "__main__":
a = LinkedList()
b = [1,2,3,4,5]
for item in b:
a.append(item)
print a
c = reverse(a)
print c
If you tag your question with Python3 please make sure it runs in python 3.
The reason is because you are mixing up points and creating an infinite loop. Print the value and it may help you find the bug. I am going to use the values to point out the issue.
while node.next:
# First node that comes in value = 1
print(node.value) #
prev = node # prev = 1
node = node.next # node = 2
new_list.head = node # You are setting the head = 2 (i.e. head = node.next)
new_list.head.next = prev # You are setting next of head = 1 (i.e. head.next = node.next.next)
# however this also set node.next.next = 1
# So going into the next loop: node.value = 2 and node.next.value = 1
Because of this pointer confusion you are forever looping between your first and second node.
This is how your reverse can look:
def reverse(linked_list):
new_list = LinkedList()
if linked_list is None:
return new_list
node = linked_list.head
new_list.head = Node(node.value)
while node.next:
node = node.next
prev_head = new_list.head
new_list.head = Node(node.value)
new_list.head.next = prev_head
return new_list
With it I got desired output of print c: [5, 4, 3, 2, 1]
General advise: create new Node instead of assignment to node in initial list.
It's a little easier to reason about this (at least to me) if you think about two references:
• One to the remaining part of the original list you haven't seen
• One to the head of the new list
At each iteration move the remaining up and set the old remaining to the head of the new list. Order is important here — as you've seen it's easy to accidentally change next on two different variables that are pointing the same node if you're not careful:
def reverse(linked_list):
new_list = LinkedList()
if linked_list is None:
return new_list
remaining = linked_list.head
while remaining:
prev_head = new_list.head # the old head becomes the first link
new_list.head = remaining # new head becomese the first remaining
remaining = remaining.next # move remaing one up the chain
new_list.head.next = prev_head # point the new head to the previous
return new_list