题目
给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。
你应当 保留 两个分区中每个节点的初始相对位置。
链接
答案
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func partition(head *ListNode, x int) *ListNode {
preBeforeX := &ListNode{}
preAfterX := &ListNode{}
tmpBefore := preBeforeX
tmpAfter := preAfterX
for head != nil {
if head.Val < x {
tmpBefore.Next = head
tmpBefore = tmpBefore.Next
} else {
tmpAfter.Next = head
tmpAfter = tmpAfter.Next
}
head = head.Next
}
tmpAfter.Next=nil // 避免回环
tmpBefore.Next = preAfterX.Next
return preBeforeX.Next
}