链表快排

func sortList(head *ListNode) *ListNode {
    slist(head,nil)
    return head
}
func slist(head *ListNode,tail *ListNode){
    if head == nil || head.Next == nil || head == tail{
        return
    }
    pivot := head.Val
    i := head
    var j *ListNode
    for j = head.Next;j != tail;j = j.Next {
        if j.Val < pivot {
            i = i.Next
            temp := j.Val
            j.Val = i.Val
            i.Val = temp
        }
    }
    temp := i.Val
    i.Val = head.Val
    head.Val = temp

    slist(head,i)
    slist(i.Next,j)
}

 

posted @ 2019-09-25 17:15  式微胡不归  阅读(156)  评论(0编辑  收藏  举报