// Things to check: // Does this work on an empty list? [] // Does this work on a singleton list? [1] // Does this work on a length 2 list? [2, 1] // Does this work on a the given input? [5, 1, 4, 2, 8] public boolean bubble() { // This is the variable we will use to keep track if we have performed a swap or not boolean swap = false; // Don't run this code unless there are at least 2 things in the list (can't bubble 0 or 1 element lists) if (front != null && front.next != null) { // Check if we need to swap the front (front is larger than next) if (front.data > front.next.data) { swap = true; // set swap to true for return // swap code (should draw before after pictrure) // before: // front -> 5 -> 1 -> 3 // after: // front -> 1 -> 5 -> 3 ListNode temp = front; front = front.next; temp.next = front.next; front.next = temp; } // Start at front so we can deal with the next two elements in the list ListNode current = front; // use a loop to travel through the adjacent pairs, keep going while there are 2 things while (current.next != null && current.next.next != null) { // same swap code as before (but uses curr.next instead of front) for the most part if (current.next.data > current.next.next.data) { swap = true; // set swap to true for return ListNode temp = current.next.next; current.next.next = temp.next; temp.next = current.next; current.next = temp; } // update current to deal with next adjacent pairs current = current.next; } } return swap; }