Linked Lists - Sorted Insert

Write a SortedInsert() function which inserts a node into the correct location of a pre-sorted linked list which is sorted in ascending order. SortedInsert takes the head of a linked list and data used to create a node as arguments. SortedInsert() should also return the head of the list.

The push() and buildOneTwoThree() functions do not need to be redefined.

Difficulty: introductory

Starter code:

def __init__(self, data):
	

Solutions:
class Node(object):
  def __init__(self, data, nxt = None):
    self.data = data
    self.next = nxt
    
def sorted_insert(head, data):
  if not head or data < head.data: return Node(data, head)
  else:
    head.next = sorted_insert(head.next, data)
    return head

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None
    
def sorted_insert(head, data):
    if head is None or head.data > data:
        return push(head, data)

    current = head
    while current.next and data > current.next.data:
        current = current.next

    current.next = push(current.next, data)
    return head

class Node(object):
  def __init__(self, data):
    self.data = data
    self.next = None
    
def sorted_insert(head, data):
  current = head;
  if head is None or head.data > data:
    return push(head, data)
  while current.next is not None and data > current.next.data:
    current = current.next
  current.next = push(current.next, data)
  return head

class Node(object):
    def __init__(self, data, next):
        self.data = data
        self.next = next

def sorted_insert(head, data):
    return Node(data, head) if not head or data < head.data else Node(head.data, sorted_insert(head.next, data))

class Node(object):
    def __init__(self, data, nxt=None):
        self.data, self.next = data, nxt


def sorted_insert(head, data):
    if head is None or head.data > data:
        return Node(data, head)
    node = head
    while node.__next__ and node.next.data < data:
        node = node.__next__
    node.next = Node(data, node.next)
    return head


class Node(object):
    def __init__(self, data, next = None):
        self.data = data
        self.next = next
        
def sorted_insert(head, data, node = None):
    if head is None or head.data >= data:
        return Node(data, head)
    if head.data < data:
        head.next = sorted_insert(head.next, data)
        return head
    raise ValueError

class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None
    
def sorted_insert(head, data):
    
    new_node = Node(data)
    if head == None or data < head.data:
        new_node.next = head
        return new_node
    node = head
    
    while 1:
        if node.next == None or data <= node.next.data:
            new_node.next = node.next
            node.next = new_node
            return head
        node = node.next

""" For your information:
class Node(object):
    def __init__(self, data):
        self.data = data
        self.next = None
"""

def sorted_insert(head, data):
    if not head:
        return Node(data)
    elif head.data>data:
        res=Node(data)
        res.next=head
        return res
    else:
        temp=head
        while head.next and head.next.data<data:
            head=head.next
        res=Node(data)
        res.next=head.next
        head.next=res
        return temp
        return temp