题解
思路:将大于 x 的节点,放到另外一个链表,最后连接这两个链表
代码
package main type ListNode struct { Val int Next *ListNode } func partition(head *ListNode, x int) *ListNode { if head == nil { return nil } headDummy := &ListNode{0, head} tailDummy := &ListNode{} head = headDummy tail := tailDummy for head.Next != nil { if head.Next.Val < x { head = head.Next } else { cnt := head.Next head.Next = head.Next.Next tail.Next = cnt tail = tail.Next } } tail.Next = nil head.Next = tailDummy.Next return headDummy.Next } func main() { }