0

As a linked list data structure learner, I'm practicing Leetcode problem "Reverse Linked List", I have my own solution but don't know why it was wrong, could anyone expert share some guidance on it? Really appreciate it!

Leetcode Question:

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL

Output: 5->4->3->2->1->NULL

My code:

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        prev = None
        while head:
            temp = head
            temp.next = prev
            head = head.next
            prev = temp
        return prev

However, the above solution is wrong but can be corrected by switching the position between "temp.next = prev" and "head = head.next" as follows:

class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        prev = None
        while head:
            temp = head
            head = head.next
            temp.next = prev
            prev = temp
        return prev

For me, there is no difference between switching the two statements or not, how come they are different?

3 Answers 3

1

Here are your references just before the loop starts:

prev --> None
head --> [something| *-]-> [something| *-]-> ...

After temp = head:

prev --> None
head --> [something| *-]-> [something| *-]-> ...
         ^
         |
temp ----+

After temp.next = prev:

prev --> None
head --> [something| *-]-> None    .... -> [something| *-]-> ...
         ^
         |
temp ----+

You no longer have any reference to the part of the list after the head. head = head.next simply sets head to None as well. prev is then set to the head of the now-truncated list.

Sign up to request clarification or add additional context in comments.

Comments

1

You have to remember that under the surface, we are dealing with pointers and the space they point too in memory.

For the first solution, head is not able to assign itself to head.next, because temp.next is now pointing to None. You've effectively reassigned head.next to null, by assigning temp.next to null.

The second solution resolves this problem by first reassigning head to head.next. This doesn't mean that temp moves along with head. Head now points to head.next, and temp points to the previous node of head (before it went to head.next).

Comments

1

temp = head makes temp refer to the same object as head

With this in mind, the next line temp.next = prev is effectively the same as head.next = prev because, again, they refer to the same object in your code.

The fix solves this beacuse before you affect the object that head refers to, you are reassigning what head refers to entirely to a different object - specifically the next in the linked list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.