+ 3

I don’t know where to fix. The more I fix it the more complicated it gets

I have a dimly linked list which I need the tail to point at the last element after it’s removed. For some reason my tail is pointing at the end of the list even after removing it. https://code.sololearn.com/cBvSOlwoc5fz/?ref=app

11th Sep 2022, 3:02 AM
Yones Mussa
1 Odpowiedź
+ 1
You just need to put this: itr.next = itr.next.next if (itr.next == None): self.tail = itr List example: a -> b -> c -> d -> None Firstly, when you remove a node, for example b, you want to cut the b's link, for that, you can just say a is pointing to c, so it will never pass by b. If the one you want to remove is the last one (d), in others words, the node is pointing to None, we want the tail being c, so we do the same as the previous example, c now will point to None, which is the d.next and d.next is c.next.next, so what we are saying is c.next = c.next.next. Now we just need to say it c.next is None then c is the tail. I hope I helped.
18th Sep 2022, 9:06 AM
Tomás Ribeiro
Tomás Ribeiro - avatar