创建2个新的链表
package main import . "nc_tools" /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param head ListNode类 * @param x int整型 * @return ListNode类 */ func partition( head *ListNode , x int ) *ListNode { // write code here if head == nil { return head } // 创建两个新的链表, 用于存储对应的节点 // dump 存储小于x的节点 // dump2 存储大于等于x的节点 dump, dump2 := &ListNode{}, &ListNode{} p1, p2 := dump, dump2 for curr := head; curr != nil; curr = curr.Next { if curr.Val >= x { p2.Next = curr p2 = curr }else{ p1.Next = curr p1 = curr } } p1.Next = dump2.Next p2.Next = nil return dump.Next }