/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem60;

/**
 * Write code to partition a linked list around a value x, such that all nodes less than x come
 * before after nodes greater than or equal to x.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class PartitionList {

}

@@ -645,16 +645,65 @@
 lem60;%0A%0A
+import com.github.pedrovgs.linkedlist.ListNode;%0A%0A
 /**%0A * W
@@ -921,10 +921,1370 @@
 List %7B%0A%0A
+  /**%0A   * Iterative algorithm to solve this problem. Using two pointer to the element before the value%0A   * and after the value we are going to create two list and merge it at the end of this algorithm.%0A   * The complexity order is equals to O(N) in time terms and O(1) in space terms because we are%0A   * not%0A   * using any auxiliary data structure to solve this problem.%0A   */%0A  public ListNode%3CInteger%3E split(ListNode%3CInteger%3E list, int x) %7B%0A    validateInput(list);%0A%0A    ListNode%3CInteger%3E before = null;%0A    ListNode%3CInteger%3E after = null;%0A%0A    while (list != null) %7B%0A      ListNode%3CInteger%3E next = list.getNext();%0A      if (list.getData() %3C x) %7B%0A        list.setNext(before);%0A        before = list;%0A      %7D else %7B%0A        list.setNext(after);%0A        after = list;%0A      %7D%0A      list = next;%0A    %7D%0A%0A    if (before == null) %7B%0A      return after;%0A    %7D%0A%0A    ListNode%3CInteger%3E result = mergeLists(before, after);%0A%0A    return result;%0A  %7D%0A%0A  private ListNode%3CInteger%3E mergeLists(ListNode%3CInteger%3E before, ListNode%3CInteger%3E after) %7B%0A    ListNode%3CInteger%3E result = before;%0A    while (before.getNext() != null) %7B%0A      before = before.getNext();%0A    %7D%0A    before.setNext(after);%0A    return result;%0A  %7D%0A%0A  private void validateInput(ListNode%3CInteger%3E list) %7B%0A    if (list == null) throw new IllegalArgumentException(%22You can't pass a null list as argument.%22);%0A  %7D%0A
 %7D%0A

Add iterative solution to problem 60