class Node: def __init__(self,data=None,next=None): self.data=data self.next=next def __repr__(self): return repr(self.data) class Linkedlist: def __init__(self): self.head=None def __repr__(self): li=[] curr=self.head while(curr): li.append(repr(curr)) curr=curr.next return "["+','.join(li)+"]" def prepend(self,data): self.head=Node(data,self.head) def append(self,data): curr=self.head if not curr: self.head=Node(data) return while(curr): if(curr.next==None): curr.next=Node(data) else: curr=curr.next lst=Linkedlist lst.prepend(1) lst.append(2) lst.append(3) print(lst)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more