/*
 * 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.problem57;

/**
 * Write code to remove duplicates from an unsorted linked list. How would you solve this problem
 * if
 * a temporary buffer is not allowed?
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class RemoveListDuplicatedElements {

}

@@ -645,16 +645,113 @@
 lem57;%0A%0A
+import com.github.pedrovgs.linkedlist.ListNode;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0A
 /**%0A * W
@@ -980,10 +980,1276 @@
 ents %7B%0A%0A
+  /**%0A   * Iterative algorithm to solve this problem. The complexity order of this algorithm in time and%0A   * space terms is O(N) where N is equals to the number of elements in the linked list.%0A   */%0A  public void remove(ListNode%3CInteger%3E head) %7B%0A    validateInput(head);%0A%0A    Set%3CInteger%3E elements = new HashSet%3CInteger%3E();%0A    ListNode%3CInteger%3E currentNode = head;%0A    ListNode%3CInteger%3E prevNode = null;%0A    while (currentNode != null) %7B%0A      if (elements.contains(currentNode.getData())) %7B%0A        prevNode.setNext(currentNode.getNext());%0A      %7D else %7B%0A        elements.add(currentNode.getData());%0A      %7D%0A      prevNode = currentNode;%0A      currentNode = currentNode.getNext();%0A    %7D%0A  %7D%0A%0A  private void validateInput(ListNode head) %7B%0A    if (head == null) %7B%0A      throw new IllegalArgumentException(%22You can't pass a null ListNode as instance.%22);%0A    %7D%0A  %7D%0A%0A  private void removeNextNodesWithValue(ListNode%3CInteger%3E srcNode) %7B%0A    ListNode%3CInteger%3E prevNode = srcNode;%0A    ListNode%3CInteger%3E currentNode = srcNode.getNext();%0A    while (currentNode != null) %7B%0A      if (currentNode.getData().equals(srcNode.getData())) %7B%0A        prevNode.setNext(currentNode.getNext());%0A      %7D%0A      prevNode = currentNode;%0A      currentNode = currentNode.getNext();%0A    %7D%0A  %7D%0A
 %7D%0A

Add iterative solution to problem 57