Sort for Linked Lists python

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 __str__(self):
    data = []
    curr = self.head
    while curr is not None:
      data.append(curr.data)
      curr = curr.next
    return "[%s]" %(', '.join(str(i) for i in data))

  def __repr__(self):
    return self.__str__()

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

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
sort python linked list program to sort a linked list in python program to sort linked list python linked list sort in python function how to sort an linked list python sort linked list py can you sort a linked list in python python how to sort linked list sort linked list in python how to sort linked list in python sorting nodes in a linked list python sort a singly linked linked list python how to sort a linked list python linked list sort python sort linked list easy sort in linked list sorting through linked lists python sort linked list in ascending order java how to use sort in linked list python c++ linked list sorting How do you sort a linked list in python python sort linked list how to sort in linked list python sort throu a linked list python sort a linked list sorting a linked list python sorting a linked list sorting of linked list javascript sortList javascript Given the head of a linked list, return the list after sorting it in ascending order. Try to sort the linked list in O(n logn) time and O(1) memory (i.e. constant space). Pythin function to sort a linked list can we sort a linked list in python python how to sort a linked list how to create a sorted link list in python algoritm to put in ascending order a linked list in python hoow to put in ascending order a linked list in python python sort linkedlist how to sort linked list python python linked list sordt sort a linked list python Sort linked list Python sort a linkedlist in python how to sort a linked list in python sort a linked list of ints python sorting a linked list in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source