6/01/2014

80. Partition List

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode l11 = new ListNode(0);
        ListNode l22 = new ListNode(0);
        ListNode l1 = l11;
        ListNode l2 = l22;
        ListNode current = head;
       
        while (current!=null) {
            if (current.val<x) {
                l1.next = new ListNode(current.val);
                l1 = l1.next;
            } else {
                l2.next = new ListNode(current.val);
                l2 = l2.next;
            }
            current = current.next;
        }
       
        l1.next = l22.next;
        return l11.next;
    }
}

没有评论:

发表评论