Skip to content

Instantly share code, notes, and snippets.

@goish135
Created August 25, 2021 11:30
Show Gist options
  • Save goish135/c94b66d3a66f4a6456ae69d8617ca7a0 to your computer and use it in GitHub Desktop.
Save goish135/c94b66d3a66f4a6456ae69d8617ca7a0 to your computer and use it in GitHub Desktop.
class Node:
def __init__(self):
self.data = None
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def addNode(self, data):
curr = self.head
if curr is None:
n = Node()
n.data = data
self.head = n
return
if curr.data > data:
n = Node()
n.data = data
n.next = curr
self.head = n
return
while curr.next is not None:
if curr.next.data > data:
break
curr = curr.next
n = Node()
n.data = data
n.next = curr.next
curr.next = n
return
def main():
ll = LinkedList()
num = int(input("Enter a number: "))
while num != -1:
ll.addNode(num)
num = int(input("Enter a number: "))
c = ll.head
while c is not None:
print(c.data)
c = c.next
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment