Permalink
Browse files

Problem 2.5 followup done

  • Loading branch information...
mdymczyk committed Feb 11, 2012
1 parent dd56b5a commit 8e513489347f726f3f99c25b3a4ca66b3013361e
@@ -5,7 +5,7 @@
public class Excercise1 {
- public static void removeDuplicated(LinkedListNode<Integer> node) {
+ public static void removeDuplicated(Node<Integer> node) {
Set<Integer> uniqueValues = new HashSet<Integer>();
if (node == null) {
@@ -24,10 +24,10 @@ public static void removeDuplicated(LinkedListNode<Integer> node) {
}
}
- public static void removeDuplicatesNoBuffer(LinkedListNode<Integer> node) {
+ public static void removeDuplicatesNoBuffer(Node<Integer> node) {
while (node != null) {
- LinkedListNode<Integer> next = node.getNext();
- LinkedListNode<Integer> lastUnique = node;
+ Node<Integer> next = node.getNext();
+ Node<Integer> lastUnique = node;
while (next != null) {
if (node.getData().equals(next.getData())) {
lastUnique.setNext(next.getNext());
@@ -40,7 +40,7 @@ public static void removeDuplicatesNoBuffer(LinkedListNode<Integer> node) {
}
public static void main(String[] args) {
- LinkedListNode<Integer> node = new LinkedListNode<Integer>(2);
+ Node<Integer> node = new Node<Integer>(2);
node.appendToTail(3).appendToTail(2).appendToTail(5);
node.printList();
System.out.println();
@@ -4,7 +4,7 @@
public class Excercise2 {
public static void main(String[] args) {
- LinkedListNode<Integer> node = new LinkedListNode<Integer>(1);
+ Node<Integer> node = new Node<Integer>(1);
node.appendToTail(3).appendToTail(2).appendToTail(5).appendToTail(7).appendToTail(9).appendToTail(4);
node.printList();
System.out.println();
@@ -13,10 +13,10 @@ public static void main(String[] args) {
System.out.println(findNthRecursive(node, 4, new IntWrapper()));
}
- private static <T> T findNth(LinkedListNode<T> node, int n) {
+ private static <T> T findNth(Node<T> node, int n) {
int size = 0;
- LinkedListNode<T> runner = node;
+ Node<T> runner = node;
while(runner != null) {
size++;
runner = runner.getNext();
@@ -29,13 +29,13 @@ public static void main(String[] args) {
return node.getData();
}
- private static <T> T findNthWithRunner(LinkedListNode<T> node, int n) {
+ private static <T> T findNthWithRunner(Node<T> node, int n) {
if(n < 1) {
return null;
}
- LinkedListNode<T> first = node;
- LinkedListNode<T> second = node;
+ Node<T> first = node;
+ Node<T> second = node;
for(int j = 0 ; j < n ; j++) {
if(second != null) {
@@ -57,7 +57,7 @@ public static void main(String[] args) {
return first.getData();
}
- private static <T> T findNthRecursive(LinkedListNode<T> node, int n, IntWrapper counter) {
+ private static <T> T findNthRecursive(Node<T> node, int n, IntWrapper counter) {
if(n < 1) return null;
if(node.getNext() == null) {
counter.counter++;
@@ -3,8 +3,8 @@
public class Excercise3 {
public static void main(String[] args) {
- LinkedListNode<Integer> node = new LinkedListNode<Integer>(1);
- LinkedListNode<Integer> toDelete = node.appendToTail(3).appendToTail(2);
+ Node<Integer> node = new Node<Integer>(1);
+ Node<Integer> toDelete = node.appendToTail(3).appendToTail(2);
toDelete.appendToTail(5).appendToTail(7).appendToTail(9).appendToTail(4);
node.printList();
@@ -16,7 +16,7 @@ public static void main(String[] args) {
}
- private static void delete(LinkedListNode<Integer> toDelete) {
+ private static void delete(Node<Integer> toDelete) {
if(toDelete == null || toDelete.getNext() == null) {
return;
}
@@ -3,61 +3,54 @@
public class Excercise4 {
public static void main(String[] args) {
- LinkedListNode<Integer> node = new LinkedListNode<Integer>(1);
+ Node<Integer> node = new Node<Integer>(1);
node.appendToTail(3).appendToTail(7).appendToTail(5).appendToTail(2)
.appendToTail(9).appendToTail(4);
node.printList();
partitionList(node, 5).printList();
partitionList2(node, 5).printList();
}
- private static LinkedListNode<Integer> partitionList2(LinkedListNode<Integer> node, int x) {
- LinkedListNode<Integer> pivot = null;
- LinkedListNode<Integer> before = null;
- LinkedListNode<Integer> after = null;
+ private static Node<Integer> partitionList2(Node<Integer> node, int x) {
+ Node<Integer> before = null;
+ Node<Integer> after = null;
while (node != null) {
- LinkedListNode<Integer> next = node.getNext();
+ Node<Integer> next = node.getNext();
if(node.getData() < x) {
node.setNext(before);
before = node;
- } else if (node.getData() > x) {
+ } else {
node.setNext(after);
after = node;
- } else {
- pivot = node;
- }
-
+ }
node = next;
}
if(before == null) {
return after;
}
- LinkedListNode<Integer> head = before;
+ Node<Integer> head = before;
while(before.getNext() != null) {
before = before.getNext();
}
- pivot.setNext(after);
- before.setNext(pivot);
+ before.setNext(after);
return head;
}
- private static LinkedListNode<Integer> partitionList(LinkedListNode<Integer> node, int x) {
- LinkedListNode<Integer> partitionNode = null;
-
- LinkedListNode<Integer> beforeStart = null;
- LinkedListNode<Integer> beforeEnd = null;
+ private static Node<Integer> partitionList(Node<Integer> node, int x) {
+ Node<Integer> beforeStart = null;
+ Node<Integer> beforeEnd = null;
- LinkedListNode<Integer> afterStart = null;
- LinkedListNode<Integer> afterEnd = null;
+ Node<Integer> afterStart = null;
+ Node<Integer> afterEnd = null;
while (node != null) {
- LinkedListNode<Integer> next = node.getNext();
+ Node<Integer> next = node.getNext();
node.setNext(null);
if(node.getData() < x) {
if(beforeStart == null) {
@@ -67,28 +60,24 @@ public static void main(String[] args) {
beforeEnd.setNext(node);
beforeEnd = node;
}
- } else if(node.getData() > x) {
+ } else {
if(afterStart == null) {
afterStart = node;
afterEnd = afterStart;
} else {
afterEnd.setNext(node);
afterEnd = node;
}
- } else {
- // only one node with value "x" supported
- // change this to a new list to support multiple nodes with value "x"
- partitionNode = node;
- }
+ }
+
node = next;
}
if(beforeStart == null) {
return afterStart;
}
- partitionNode.setNext(afterStart);
- beforeEnd.setNext(partitionNode);
+ beforeEnd.setNext(afterStart);
return beforeStart;
}
@@ -1,24 +1,25 @@
package pl.dymczyk.linkedlists;
+
public class Excercise5 {
public static void main(String[] args) {
- LinkedListNode<Integer> node1 = new LinkedListNode<Integer>(7);
+ Node<Integer> node1 = new Node<Integer>(7);
node1.appendToTail(1).appendToTail(6);
node1.printList();
- LinkedListNode<Integer> node2 = new LinkedListNode<Integer>(5);
+ Node<Integer> node2 = new Node<Integer>(5);
node2.appendToTail(9);
node2.printList();
addTwoNumbers(node1, node2).printList();
addNumbersRecursive(node1, node2, 0).printList();
}
-
+
/*********************************************************
* Recursive solution. Shorter but more memory consuming.*
*********************************************************/
- private static LinkedListNode<Integer> addNumbersRecursive(
- LinkedListNode<Integer> one, LinkedListNode<Integer> two, int carry) {
+ private static Node<Integer> addNumbersRecursive(
+ Node<Integer> one, Node<Integer> two, int carry) {
if (one == null && two == null && carry == 0) {
return null;
}
@@ -32,7 +33,7 @@ public static void main(String[] args) {
sum += two.getData();
}
- LinkedListNode<Integer> node = new LinkedListNode<Integer>(sum % 10);
+ Node<Integer> node = new Node<Integer>(sum % 10);
if (one != null || two != null || sum > 9) {
node.setNext(addNumbersRecursive(
@@ -47,14 +48,14 @@ public static void main(String[] args) {
* Iterative. Much longer but less memory consuming. *
*********************************************************/
- private static LinkedListNode<Integer> addTwoNumbers(
- LinkedListNode<Integer> one, LinkedListNode<Integer> two) {
- LinkedListNode<Integer> head = new LinkedListNode<Integer>(0);
- LinkedListNode<Integer> result = head;
+ private static Node<Integer> addTwoNumbers(
+ Node<Integer> one, Node<Integer> two) {
+ Node<Integer> head = new Node<Integer>(0);
+ Node<Integer> result = head;
while (one != null || two != null) {
int sum = addValues(one, two);
- LinkedListNode<Integer> nextResult;
+ Node<Integer> nextResult;
if (sum < 10) {
boolean isLast = isLast(one, two);
nextResult = nextResult(result, sum, 0, isLast);
@@ -71,35 +72,35 @@ public static void main(String[] args) {
return head;
}
- private static boolean isLast(LinkedListNode<Integer> one,
- LinkedListNode<Integer> two) {
+ private static boolean isLast(Node<Integer> one,
+ Node<Integer> two) {
if ((one != null && one.getNext() != null)
|| (two != null && two.getNext() != null)) {
return false;
}
return true;
}
- private static LinkedListNode<Integer> nextResult(
- LinkedListNode<Integer> result, int sum, int i, boolean isLast) {
+ private static Node<Integer> nextResult(
+ Node<Integer> result, int sum, int i, boolean isLast) {
result.setData(result.getData() + sum);
if (isLast) {
return null;
}
- return new LinkedListNode<Integer>(i);
+ return new Node<Integer>(i);
}
- private static LinkedListNode<Integer> getNext(LinkedListNode<Integer> node) {
+ private static Node<Integer> getNext(Node<Integer> node) {
if (node != null) {
return node.getNext();
}
return null;
}
- private static int addValues(LinkedListNode<Integer> one,
- LinkedListNode<Integer> two) {
+ private static int addValues(Node<Integer> one,
+ Node<Integer> two) {
if (one == null) {
return two.getData();
} else if (two == null) {
@@ -1,47 +0,0 @@
-package pl.dymczyk.linkedlists;
-
-public class LinkedListNode<T> {
-
- private LinkedListNode<T> next;
- private T data;
-
- public LinkedListNode(T data) {
- this.data = data;
- }
-
- public LinkedListNode<T> appendToTail(T data) {
- LinkedListNode<T> tail = new LinkedListNode<T>(data);
- LinkedListNode<T> node = this;
- while(node.getNext() != null) {
- node = node.getNext();
- }
- node.next = tail;
- return tail;
- }
-
- public void printList() {
- LinkedListNode<T> node = this;
- while (node != null) {
- System.out.print(node.getData() + " ");
- node = node.getNext();
- }
- System.out.println();
- }
-
- public LinkedListNode<T> getNext() {
- return next;
- }
-
- public void setNext(LinkedListNode<T> next) {
- this.next = next;
- }
-
- public T getData() {
- return data;
- }
-
- public void setData(T data) {
- this.data = data;
- }
-
-}

0 comments on commit 8e51348

Please sign in to comment.