簡體   English   中英

從LinkedList刪除“鏈接”?

[英]Removing “links” from a LinkedList?

我有一個LinkedList (為自己的代碼LinkedList )與char “在IT方面。 這是完整的列表: ['a','b','I','d','R','A','7','p']

我正在嘗試編寫一種方法,該方法將刪除不是大寫字母的所有字符。 運行該方法后, LinkedList應該看起來像這樣['I','R','A']

但是在運行代碼后,我得到與return相同的列表,該列表: ['a','b','I','d','R','A','7','p']

這是我的方法代碼:

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;
}

這是isEmpty()

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

這是ListNode

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

我可以看到搜索部分正在運行,但是我無法正確刪除節點部分,有什么建議嗎?

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;
}

您需要“更改”列表,因此您只是錯過了對元素的分配,而不是局部變量

但是,此代碼將不起作用,因為它只是分配給下一個元素,而沒有仔細檢查下一個元素是否是一個好的元素,然后跳到該元素

編輯:完整的工作代碼

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();
    }

}

ptr是局部變量,所以

ptr = ptr.next.next;

不會修改您的鏈接列表。

您應該改為修改ptr.next 除此之外,如果原始head不引用大寫字母,則可能需要修改head

這樣的事情應該工作:

// find the first valid (upper case) element and set head to refer to it
while (!Character.isUpperCase(head.element) && head != null)
    head = head.next;
ListNode ptr = head; 
if (ptr != null)
    // eliminate all non-upper case elements
    while(!isEmpty(ptr.next)){
        if(!Character.isUpperCase(ptr.next.element)){
            ptr.next = ptr.next.next;
        }
        ptr = ptr.next;
    }
}
return head;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM