1

I am a beginner, below is the best that I could come up so far. The objective of the method .Split() is that I am supposed to rearrange (rearrange only, no adding or removing nodes nor switching data of the nodes) the elements of the LinkedIntList <- This is not that standard LinkedList class, its a modified class that I have to use, there is no Java method that I can use for this problem unfortunately.

For example, if a linkedlist is [1, 5, -9, 7, -5, 78], the result should be [-9,-5, 1, 5, 7, 78]. The order of the negative values do not matter.

There is only one private field, which is ListNode front;

I am trying to use current.data < 0 to pick out the negative values and put them at the front of the list, iterating from the front, and start over. Making sure that it selects all the negative values from the list. But my output keeps getting stuck in a endless loop, unable to break. I am not sure what to do here.

Here is my best attempt at this problem.

public void Split()
{
ListNode current = front;
ListNode head = front;
    
    while (current.next != null)
    {
        if (current.data < 0)
        {
            current.next = front; // set the pointer of current's node
            front = current; // update the head with a negative value
            current = current.next; // iterate from the front 
        }
        else if (current.data >= 0)
        {
            current = current.next; // if positive, skip the process and keep iterating
        }
        else
        {
            break; // break if null or anything else
        }
        
        if (current.next == null || current == null)
        {
            break; // another attempt to break since it keeps getting stuck
        }
    }
}
Max
  • 31
  • 4

2 Answers2

1

hi llda: in order to move negative numbers ahead, I suggest get it done via two adjacent pointers. See the demo idea picture attached.

The code shows below:

public void Split(){
    ListNode current = front.next;
    ListNode prior = front;
    ListNode head = front;

    do {
        if(current.data < 0 && current.next != null){
            prior.next = current.next;
            head = current;
            head.next = front;
            current = prior.next;
        }

        current = current.next;
        prior = prior.next;
    } while(current.next != null)
}

enter image description here

forrestg
  • 327
  • 5
  • 13
  • it seems like this one removes one of the negative numbers and does not add them at the front, while the other negative number stays the same. But I appreciate your demo and your explanation and will try to focus on my code the way you explained it – Max Feb 15 '21 at 23:19
1

You have to update all related references to keep the linked list intact.

As a homemade implementation, I would suggest a full sort (a kind of bubblesort) this way:

...

    public void sort() {
        ListNode item = front;

        while (item.next != null) {
            if (needSwapping(item, item.next)) {
                item = swapItems(item, item.next);
                if (item.prev != null) {
                    // one step back to check next against prev in next loop
                    item = item.prev;
                }
            } else {
                item = item.next;
            }
        }

        front = firstNode(item);
    }

    private ListNode swapItems(ListNode item, ListNode next) {
        ListNode outerPrev = item.prev;
        ListNode outerNext = next.next;

        // change the refs of outer elements
        if (outerPrev != null) {
            outerPrev.next = next;
        }
        if (outerNext != null) {
            outerNext.prev = item;
        }

        // change refs to outer elements
        item.next = next.next;
        next.prev = item.prev;

        // change inner refs
        item.prev = next;
        next.next = item;

        return next;
    }

    private boolean needSwapping(ListNode item, ListNode next) {
        return item.data > next.data;

        // here is the point for optimizations of sort accuracy eg. negatives order doesn't matter
        // return item.data >0 && item.data > next.data;
    }


...

But much more modern Java Style would be to implement the Comparable interface or to provide a Comparator and make use of the Collections.sort method as described here or at stackoverflow.

Mike Feustel
  • 1,277
  • 5
  • 14