Ant
Ant

Reputation: 1153

I don't understand why these two classes output different things

I don't understand why these near identical classes with almost the same functions output different things.
They are both supposed to return a reversed array by reading them as linked lists.
The first one outputs the reversed array but the second one outputs the the memory location.

I would like for the second class below to output the reversed array just like the first class does.

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def isPalindrome(self, head):
        vals = []
        while head:
            vals += head.val,
            head = head.next
        return vals[0][::-1]

head = [1,2,3,4,5]
Solution().isPalindrome(ListNode(head))  

outputs [5, 4, 3, 2, 1]

# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def isPalindrome(self, head):
        prev = None
        while head:
            curr = head
            head = head.next
            curr.next = prev
            prev = curr
        return prev  

head = [1,2,3,4,5]
Solution().isPalindrome(ListNode(head))  

outputs <__main__.ListNode at 0x7f49e28afa00>

Upvotes: 0

Views: 48

Answers (2)

Z Li
Z Li

Reputation: 4318

This is because you returned a ListNode instance. Which is printed as <__main__.ListNode at 0x7f49e28afa00>

To print the reversed linked list, you need an extra step to print the already reversed list.

Also note that your test code is wrong. head = [1,2,3,4,5] and do ListNode(head) does not create a valid linked list. It only creates a list node with val [1,2,3,4,5]

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def isPalindrome(self, head):
        prev = None
        while head:
            curr = head
            head = head.next
            curr.next = prev
            prev = curr
        
        l = []
        while prev is not None:
            l.append(prev.val)
            prev = prev.next
        return l

# This creates a valid linked list for testing
head = ListNode(1)
cur = head
for i in range(2, 6):
    cur.next = ListNode(i)
    cur = cur.next
    
Solution().isPalindrome(head) 
[5, 4, 3, 2, 1]

Upvotes: 1

Luca Angioloni
Luca Angioloni

Reputation: 2253

Your first implementation basically does nothing. The only line of code that is reversing the list is:

vals[0][::-1]

because in general given a list:

some_list = [1,2,3,4]
print(some_list[::-1]) # prints [4, 3, 2, 1]

So the ListNode class is useless and so is the other one. You are just creating one ListNode who's value is the entire list.

In the second implementation instead you are using these classes and at the end you only return one node and that's what you print.

You should walk through the resulting linked list and compose the list to be printed or print elements while you move.

Upvotes: 0

Related Questions