Need help in understanding iterations in linked list

Hello, I’m working on two-pointer linked list techniques(https://www.codecademy.com/paths/pass-the-technical-interview-with-python/tracks/linear-data-structures-python/modules/linked-list-practice-python/articles/two-pointer-linked-list-techniques-python), my code is running fine on the Codecademy playground but when I try to make a similar code anywhere else it runs an iteration extra. For example:

def n_th_last_node(linked_list,n): resultNode=None tailSeeker=linked_list.headNode index=1 while tailSeeker is not None: tailSeeker=tailSeeker.nextNode index+=1 if index>= n + 1: if resultNode is None: resultNode=linked_list.headNode else: resultNode=resultNode.nextNode return resultNode ll=LinkedList() ll.addNode(5) ll.addNode(4) ll.addNode(3) ll.addNode(2) ll.addNode(1) ll.stringify() nThLast=n_th_last_node(ll,2)

when I run this code on Codecademy, I’m getting 4 but when I run the same code on my local machine I get 5, after debugging I noticed that it is running an extra iteration on the while statement. Any idea, what’s the issue and how to resolve it

Could be a couple of things. I can’t read your code too well at the moment because this codebyte feature is a bit cramped for my taste (can’t seem to adjust it).

But from what I can tell, you’re instantiating a LinkedList. Is it defined the same in both your local environment and the CC environment? Are you using the same versions of python on both? These are the first things I think about when results vary like this.

1 Like

Yes, I’m facing this weird problem where all of my loop is running one extra iteration. In this code, the loop isn’t stopping when the value of curr1 and curr2 are the same. Thanks

def findIntersection(ll1,ll2): print(ll1.headNode) curr1=ll1.headNode curr2=ll2.headNode while curr1!=curr2: if curr1==None: curr1=ll2.headNode else: curr1=curr1.nextNode if curr2 == None: curr2=ll1.headNode else: curr2=curr2.nextNode return curr1

don’t use the codebyte for complicated things, just put 3 backticks before and after, this link explains the formatting: [How to] Format code in posts
I’m not sure what the answer to your question is though.

def n_th_last_node(linked_list,n):

    resultNode=None

    tailSeeker=linked_list.headNode

    index=1

    while tailSeeker is not None:

        tailSeeker=tailSeeker.nextNode

        index+=1

        if index>= n + 1:

            if resultNode is None:

                resultNode=linked_list.headNode

            else:

                resultNode=resultNode.nextNode

    return resultNode
ll=LinkedList()

ll.addNode(5)

ll.addNode(4)

ll.addNode(3)

ll.addNode(2)

ll.addNode(1)

ll.stringify()

nThLast=n_th_last_node(ll,2)

If the while loop is not stopping it means the values are not the same. You should double check your conditional flow to make sure your operations are clean.

Meaningful formatted print statements can help unravel that.

Hello,
looks like there’s no dedicated post for this exercise of the python:technical interview course, so I continue here since it is the same subject.

I try my hand on a solution to find the the nth last element of a singly linked list, before to move on the solution, I would be very please to have a point of view, critic of a pythonista about my code in term efficiency, big O.


    def runner(self, n_last):
        total_index = 0
        index_n_last = 0
        current_node = self.head_node

        while current_node:

            if current_node.get_next_node() == None:
                break
            else:
                current_node = current_node.get_next_node()
                total_index += 1


        index_n_last = total_index - n_last

        current_index = 0
        current_node = self.head_node

        while current_node:

            if current_index == index_n_last:
                return current_node.get_value()
                break
            else:
                current_node = current_node.get_next_node()
                current_index += 1

I think you could strip some variables out

    def runner(self, n_last):
        total_index = 0
        current_node = self.head_node

        while current_node:

            if current_node.get_next_node() == None:
                break
            else:
                current_node = current_node.get_next_node()
                total_index += 1


        total_index -= n_last
        current_node = self.head_node

        while current_node:

            if total_index == 0:
                return current_node.get_value()
                break
            else:
                current_node = current_node.get_next_node()
                total_index -= 1
1 Like

Indeed, nice short cut! Thank you very much.

So after my own solution, I move on the one proposed by codeAcademy and I have the same issue than [254utkarsh]:

An offset of 1 on the result expected. For example if you have a linkedList of 3 nodes, A-B-C, with a nth_last of 2, you would expect the node B as result and you l will have this if you run the code on this website.
But If I run the same code on local, I will have the node C .

If I want to get the same result localy, I have to change :

if count >= n + 1:

to

if count >= n + 2:

My local codes experiemtations are always match what I code here on this website.

I still have a issue with this serie of exercises. Now with the find_middle method .(https://www.codecademy.com/paths/pass-the-technical-interview-with-python/tracks/linear-data-structures-python/modules/linked-list-practice-python/articles/two-pointer-linked-list-techniques-python)

I put the code in Python Tutor - Visualize Python, Java, JavaScript, C, C++, Ruby code execution.
And the output isn’t the one expected. I have localy, the same output than the pythontutor visualizer. Both differs from the one from Codeacademy.


Can someone confirm he is able to make this course works localy?

Have you checked if you’re running the same version of python?

I use Python 3.8.5 on my desktop (I typed in a command prompt window: python)
I also run this code on a different python online compiler: Online Python Compiler (Interpreter) and the result is the same.

Yea, so that’s not the same as what CC is running, which is 3.6.9.
This may or may not be what’s causing the issue but I’d set up an environment with the same version when testing code with multiple platforms.

1 Like

ok, I guess I will have to take time to install differents versions of python on my computer in a soon future.

I found on stack overflow than the 3.6.9 was a security update, and the 3.7 is retro compatible with it, (Compiling Python 3.6.9 on Windows - Stack Overflow) I will give a try with this one.

Thanks for taking time to check my issue.

Often it’s time for minor things but I just found over time it’s a pain if that’s the actual source of the error (it still may not be). You could say it’s good practice to use virtual environments with python. I use venv but I hear great things about pipenv.