package com.thinker.test.test2;

/**
 *
 * @author lzh
 *
 **/

public class Partition{

	public ListNode partition(ListNode pHead, int x){
		if(null == pHead) return null;
		ListNode preStart = null;
		ListNode preEnd = null;
		ListNode afterStart = null;
		ListNode afterEnd = null;

		while(pHead != null){
			if(pHead.val < x){
				if(preStart == null){
					preStart = pHead;
					preEnd = preStart;
				}else{
					preStart.next = pHead;
					preEnd = pHead;
				}
			}else{
				if(afterEnd == null){
					afterStart = pHead;
					afterEnd = pHead;
				}else{
					afterEnd.next = pHead;
					afterEnd = pHead;
				}				
			}
			pHead = pHead.next;
		}
		if(preStart == null) return afterStart;
		preEnd.next = afterStart;
		return preStart;
	}

	public static void main(String[] args){

	}
}
class ListNode{
	int val;
	ListNode next;
	public ListNode(int val){
		this.val = val;
	}
}
