Created
June 23, 2014 20:01
-
-
Save UncleGarden/c5b19fbe945b21a67846 to your computer and use it in GitHub Desktop.
CareerCup 150
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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