#🔒 Reverse and To_Plain_List Help

20 messages · Page 1 of 1 (latest)

real hamlet
#

I need help with my reverse and to_plain_list functions. I'm not sure I understand how to make them recursive and then not exceed maximum recursion depth.


    def reverse(self, current=None, previous=None):
        """Reverses the order of nodes in the linked list."""
        if current is None:
            current = self._head

        if current is not None:
            next_node = current.next
            current.next = previous
            self._head = self.reverse(next_node, current)

    def to_plain_list(self, current=None):
        """Returns a regular list with the same values as the linked list."""
        if current is None:
            current = self._head

        if current is None:
            return []
        else:
            return [current.data] + self.to_plain_list(current.next)
fervent cometBOT
#

@real hamlet

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

real hamlet
stable umbra
#
    def to_plain_list(self, current=None):
        """Returns a regular list with the same values as the linked list."""
        if current is None:
            current = self._head

        if current is None:
            return []
        else:
            return [current.data] + self.to_plain_list(current.next)

you are setting your current to the head of your linked list despite it being None. while* this is ok to handle calling the function without passing in current, when you reach your last node, current is actually None again, which is set back to self._head again, and the recursion is thus not able to end

#

if we want to continue following this pattern you have here, what i would do is instead to catch current.next is None instead

#
    def to_plain_list(self, current=None):
        """Returns a regular list with the same values as the linked list."""
        if current is None:
            current = self._head

        if current.next is None:
            return [current.data]
        else:
            return [current.data] + self.to_plain_list(current.next)
#

for reverse,
self._head = self.reverse(next_node, current) sets _head to the result of the recursive call, which might not actually be the new head of the reversed list.

#

you need to check if u are actually at the end of the list, and set the final result of the recursion to head

#
    def reverse(self, current=None, previous=None):
        """Reverses the order of nodes in the linked list."""
        if current is None:
            current = self._head

        if current is not None:
            next_node = current.next
            current.next = previous
            if next_node is None:
                self._head = current
            else:
                self.reverse(next_node, current)
real hamlet
#

Oh okay so I'm making the if statement true again which repeats the whole thing.

real hamlet
#

Just with like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

#

I get that that's already a normal list but just demonstrate the order of what's changing here please.

stable umbra
#

the logging is abit overly verbose but u can kind of understand whats going on

real hamlet
stable umbra
fervent cometBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.