美文网首页Leetcodeleetcode
86. Partition List.go

86. Partition List.go

作者: AnakinSun | 来源:发表于2019-03-24 20:56 被阅读0次

新建两个list,然后遍历原来的list,不断的填充

func partition(head *ListNode, x int) *ListNode {

    beforehead := &ListNode{}
    before := beforehead
    afterhead := &ListNode{}
    after := afterhead
    for head != nil {
        if head.Val < x {
            before.Next = head
            before = before.Next
        } else {
            after.Next = head
            after = after.Next
        }
        head = head.Next
    }
    after.Next = nil
    before.Next = afterhead.Next
    return beforehead.Next
}

相关文章

网友评论

    本文标题:86. Partition List.go

    本文链接:https://www.haomeiwen.com/subject/dujpvqtx.html