Skip to content

Commit

Permalink
Problem 2.5 followup done
Browse files Browse the repository at this point in the history
  • Loading branch information
mdymczyk committed Feb 11, 2012
1 parent dd56b5a commit 8e51348
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 111 deletions.
10 changes: 5 additions & 5 deletions src/pl/dymczyk/linkedlists/Excercise1.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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());
Expand All @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions src/pl/dymczyk/linkedlists/Excercise2.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -29,13 +29,13 @@ private static <T> T findNth(LinkedListNode<T> node, int n) {
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) {
Expand All @@ -57,7 +57,7 @@ private static <T> T findNthWithRunner(LinkedListNode<T> node, int n) {
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++;
Expand Down
6 changes: 3 additions & 3 deletions src/pl/dymczyk/linkedlists/Excercise3.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}
Expand Down
49 changes: 19 additions & 30 deletions src/pl/dymczyk/linkedlists/Excercise4.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -67,28 +60,24 @@ private static LinkedListNode<Integer> partitionList(LinkedListNode<Integer> nod
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;
}
Expand Down
39 changes: 20 additions & 19 deletions src/pl/dymczyk/linkedlists/Excercise5.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand All @@ -32,7 +33,7 @@ private static LinkedListNode<Integer> addNumbersRecursive(
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(
Expand All @@ -47,14 +48,14 @@ private static LinkedListNode<Integer> addNumbersRecursive(
* 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);
Expand All @@ -71,35 +72,35 @@ private static LinkedListNode<Integer> addTwoNumbers(
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) {
Expand Down
47 changes: 0 additions & 47 deletions src/pl/dymczyk/linkedlists/LinkedListNode.java

This file was deleted.

0 comments on commit 8e51348

Please sign in to comment.