sort for linked lists python

Solutions on MaxInterview for sort for linked lists python by the best coders in the world

showing results for - "sort for linked lists python"
Sofie
04 Feb 2017
1class Node:
2  def __init__(self):
3    self.data = None
4    self.next = None
5
6class LinkedList:
7  def __init__(self):
8    self.head = None
9
10  def addNode(self, data):
11    curr = self.head
12    if curr is None:
13      n = Node()
14      n.data = data
15      self.head = n
16      return
17
18    if curr.data > data:
19      n = Node()
20      n.data = data
21      n.next = curr
22      self.head = n
23      return
24
25    while curr.next is not None:
26      if curr.next.data > data:
27        break
28      curr = curr.next
29    n = Node()
30    n.data = data
31    n.next = curr.next
32    curr.next = n
33    return
34
35  def __str__(self):
36    data = []
37    curr = self.head
38    while curr is not None:
39      data.append(curr.data)
40      curr = curr.next
41    return "[%s]" %(', '.join(str(i) for i in data))
42
43  def __repr__(self):
44    return self.__str__()
45
46def main():
47  ll = LinkedList()
48  num = int(input("Enter a number: "))
49  while num != -1:
50    ll.addNode(num)
51    num = int(input("Enter a number: "))
52  c = ll.head
53  while c is not None:
54    print(c.data)
55    c = c.next
queries leading to this page
program to sort a linked list in pythonlinked list sort in python functionsorting a linked list pythonpythin function to sort a linked listhow to sort linked list pythonsort linked listsort a linked listhow to create a sorted link list in pythonprogram to sort linked list pythonhow to use sort in linked list pythonhow do you sort a linked list in pythoncan we sort a linked list in pythonsort a linkedlist in pythonhow to sort an linked list pythonhow to sort linked list in pythonsorting of linked list javascriptsorting a linked listhow to sort a linked list pythonsort linked list in ascending order javapython sort linkedlisthow to sort a linked list in pythonsortlist javascript given the head of a linked list 2c return the list after sorting it in ascending order try to sort the linked list in o 28n logn 29 time and o 281 29 memory 28i e constant space 29 python how to sort a linked listsort python linked list hoow to put in ascending order a linked list in pythonsort a linked list in pythonalgoritm to put in ascending order a linked list in pythonsorting nodes in a linked list pythonlinked list sort pythonpython sort linked listsorting of linked list program in pythonsort linked list in pythonsort a linked list pythonsort linked list pysort a linked list of ints pythonsort linked list pythonpython how to sort linked listsort a singly linked linked list pythonsorting a linked list in pythonsorting through linked lists pythoneasy sort in linked listpython linked list sordtc 2b 2b linked list sortingsort throu a linked list pythonhow to sort in linked list pythoncan you sort a linked list in pythonsorting linked list pythonsort for linked lists python