Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<>();
- list.add(10);
- list.add(1);
- list.add(30);
- list.add(-4);
- list.add(-5);
- list.add(20);
- list.add(2);
- partition(list, 8);
- for (Integer i : list) {
- System.out.println(i);
- }
- }
- static void partition(LinkedList<Integer> list, int val) {
- int start = 0;
- int endIndex = list.size() - 1;
- while (true) {
- if (start >= endIndex) {
- break;
- }
- while (list.get(start) < val) {
- start++;
- }
- while (list.get(endIndex) > val) {
- endIndex--;
- }
- if (start >= endIndex) {
- break;
- }
- // swap elements at start and endPointer
- Integer temp = list.get(start);
- list.set(start, list.get(endIndex));
- list.set(endIndex, temp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment