Update next LIST ADT header without using an iterator

How do I upgrade to a new node chapter without using the following?

private E head;
private someList<E> tail;

public E removeHead(){
        this.tail = this.tail.getTail();  
        return this.head;
    }
+4
source share
1 answer

I think this is what you want?

public E removeHead(){
    E oldhead = this.getHead();  // keep old head, so we can return it
    this.head = this.getTail().getHead(); // new head: comes out of old tail
    this.tail = this.getTail().getTail(); // new tail: remainder of old tail
    return oldhead;
}

public getHead(){ return head;}

public getTail(){ return tail;}

I assume that you have already implemented getTail?

+1
source

Source: https://habr.com/ru/post/1606021/


All Articles