/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func insertionSortList(head *ListNode) *ListNode { if head==nil{ return head } //定一个前置节点 dummy:=new(ListNode) cur:=head for cur!=nil{ //每次从前面的节点寻找大于cur的节点,插入此节点前面 pre:=dummy for pre.Next!=nil&&pre.Next.Val<cur.Val{ pre = pre.Next } next:=cur.Next //cur 放pre前面 cur.Next = pre.Next pre.Next = cur cur = next } return dummy.Next }