Carlton Carlton - 4 months ago 11
Java Question

Removing "links" from a LinkedList?

I have a

LinkedList
(Own code for the
LinkedList
) with
char
's in it. This is the complete list:
['a','b','I','d','R','A','7','p']
.

I am trying to write a method which will deleting all characters that is NOT a upper case letter. After running the method, the
LinkedList
should look like this
['I','R','A']
.

But after running my code I get the same list as
return
, this list:
['a','b','I','d','R','A','7','p']
.

Here is my code for the method:

public static ListNode copyUpperCase(ListNode head) {

ListNode ptr = head;
while(!isEmpty(ptr.next)){
if(!Character.isUpperCase(ptr.element)){
ptr = ptr.next.next;
//System.out.println(ptr.element);
}
ptr = ptr.next;
}
return head;
}


Here is
isEmpty()
:

public static boolean isEmpty(ListNode l) {
if ( l == null )
throw new ListsException("Lists: null passed to isEmpty");
return l.next == null;
}


Here is
ListNode
:

public class ListNode {
public char element;
public ListNode next;
}


I can see that the search part is working, but I can't get the deleting the node part right, any suggestions?

Answer
public static ListNode copyUpperCase(ListNode head) {
    ListNode ptr = head;
    while(!isEmpty(ptr.next)){
        if(!Character.isUpperCase(ptr.element)){
            ptr.next = ptr.next.next;
            //System.out.println(ptr.element);
        }
        ptr = ptr.next;
    }
    return head;
}

You need to "CHANGE" the list, so you just missed the assignment to the element and not the the local variable

THIS code will not work however, as this just assign to the next element, without looking if next element actully is a good one, and then skips to that one

Edit: full working code

class ListNode {

    public ListNode(char element,ListNode next ) {
        this.element = element;
        this.next = next;
    }

    public char element;
    public ListNode next;

    void print() {
        System.out.print(this.element+",");
        if(this.next != null) {
            this.next.print();
        }
        else {
            System.out.println("");
        }

    }

}
public class main {


    //Imo you should only check if this elem is a null one, as a null means empty, a null on next only means that it's the last elem, but will still contain data
    public static boolean isEmpty(ListNode l) {
        return l == null;
    }

    public static ListNode getNextUpper(ListNode head) {
        while(!isEmpty(head)){
            if(Character.isUpperCase(head.element)) {
                return head;
            }
            head = head.next;
        }
        return null;
    }

    public static ListNode copyUpperCase(ListNode head) {
        ListNode newhead = getNextUpper(head);
        ListNode temp = newhead;
        while(!isEmpty(temp)){
            temp.next = getNextUpper(temp.next);
            temp = temp.next;
        }
        return newhead;
    }

    public static void main(String[] args) {
        ListNode t = new ListNode('a' , new ListNode('b' , new ListNode('I', new ListNode('d', new ListNode('R', new ListNode('A', new ListNode('7', new ListNode('p',null))))))));

        t.print();

        ListNode newt = copyUpperCase(t);

        newt.print();
    }

}