# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = NoneclassSolution(object):definsertionSortList(self,head):""" :type head: ListNode :rtype: ListNode """ifhead==Noneorhead.next==None:returnheadnew_node=ListNode(-sys.maxint-1)# new_node.next = headnew_node.next=headhead=new_node# tail: biggest element current sorted listtail=headwhiletail.next:cur=tail.next# case: just assign new node after the tailifcur.val>=tail.val:tail=tail.nextelse:# case: need to insert the node again# find the placeprev=headnew_cur=prev.nextwhilenew_cur.val<cur.val:new_cur=new_cur.nextprev=prev.next# remove the nodetail.next=cur.next# insert the node againcur.next=prev.nextprev.next=curreturnhead.nextreturnhead.