Skip to content

Instantly share code, notes, and snippets.

@UncleGarden
Created June 23, 2014 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UncleGarden/c5b19fbe945b21a67846 to your computer and use it in GitHub Desktop.
Save UncleGarden/c5b19fbe945b21a67846 to your computer and use it in GitHub Desktop.
CareerCup 150
/**
* 2.4 Write code to partition a linked list around a value x, such that all
* nodes less than x come before all nodes greater than or equal to x.
*
* @author Jiateng
*/
public class CC2_4 {
public static LinkedList partition(LinkedList list, int value) {
LinkedList less = new LinkedList();
LinkedList notLess = new LinkedList();
for (int i = 0; i < list.size(); i++) {
if ((int) list.get(i) < value) {
less.add(list.get(i));
} else {
notLess.add(list.get(i));
}
}
less.addAll(notLess);
return less;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(10);
list.add(9);
list.add(8);
list.add(7);
list.add(6);
list.add(5);
list.add(4);
list.add(3);
list.add(2);
list.add(1);
LinkedList newList = partition(list, 5);
System.out.println(newList.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment