the object variable in link list reverse codes -


hi link list reverse code. can me understand differecen between r = solution().reverselist(n1) , r = solution().reverselist(listnode(1)).

i wonder why output [1] , [3,2,1] respectively , purpose of assign listnode object variable.

class solution(object): def reverselist(self, head):     """     :type head: listnode     :rtype: listnode     """     #if not head:     #   return none     prev = head     curr = prev.next     while curr:         next =curr.next         curr.next = prev         prev = curr         curr = next     head.next = none     return prev     class listnode(object):     def __init__(self,x):         self.val = x         self.next = none     def to_list(self):         return[self.val] + self.next.to_list() if self.next else [self.val]  if __name__ == "__main__":     n1 = listnode(1)     n2 = listnode(2)     n3 = listnode(3)     n1.next = n2     n2.next = n3     r = solution().reverselist(n1)     print r.to_list()     #assert r.to_list() == [3,2,1] 

i understood now.

listnode(1) has no "next" set, forever return 1 it's list, whereas n1 starts same way (as node of 1), "next" attribute set node of 2 "next" set node of 3, n1 carries of information it, , listnode(1) not.

welcome comments.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -