Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

PREVIOUS NEXT
Code Example
Python :: shared SHMEM python 
Python :: python indent print 
Python :: python linear search 
Python :: pandas append sheet to workbook 
Python :: create a list of pandas index 
Python :: sklearn tree visualization 
Python :: docker run python 
Python :: pandas groupby 
Python :: pandas print column by index 
Python :: supress jupyter notebook output 
Python :: design patterns python 
Python :: binary search tree implementation in python 
Python :: how to implement heap in python 
Python :: how to set geometry to full screen in pyqt5 
Python :: index.py:14: RuntimeWarning: invalid value encountered in true_divide return np.dot(user, user2) / (norm(user) * norm(user2)) 
Python :: Working with WTForms FieldList 
Python :: importing time and sleep. python 
Python :: stock market python 
Python :: django form action 
Python :: label_map dict = label_map_util.get_label_map_dict(label_map) 
Python :: pandas convert column to title case 
Python :: how to display items on a list on new lines python 
Python :: not intersection list python 
Python :: regex find all sentences python 
Python :: python cant remove temporary files 
Python :: correlation meaning 
Python :: ** in python 
Python :: mean pandas 
Python :: using hashlib module in python 
Python :: how to put space in between list item in python 
ADD CONTENT
Topic
Content
Source link
Name
3+8 =