I see a linked list class definition in Leetcode:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = None
I am confused how to use it, there are two methods I want to figure out:
Firstly: if I want to create a linked list 1->3->5 step by step, how? My idea is that:
p1 = ListNode(1)
p2 = ListNode(3)
p3 = ListNode(5)
p2 = p1.next
p3 = p1.next.next
print(p1.val) #however it shows p1 doesn't have next attr...
Secondly, if I have a linked list, like that:
l1 = ListNode([1, 2, 4, 6])
How to get every value step by step? I tried like:
print(l3.next)
print(l3.next.next)#maybe just some false idea...