双链表中的removeFirst节点

时间:2016-08-16 11:23:30

标签: java

我找到了关于双向链表的代码。

/**
 * this method removes element from the start of the linked list
 * @return
 */
public E removeFirst() {
    if (size == 0) throw new NoSuchElementException();
    Node tmp = head;
    head = head.next;
    head.prev = null;
    size--;
    System.out.println("deleted: "+tmp.element);
    return tmp.element;
}

以上代码来自此处:http://java2novice.com/data-structures-in-java/linked-list/doubly-linked-list/

我用另一种方式重写了这段代码:

public E removeFirst() {
    if (head == null) {
        return null;
    }
    Node tmp = head.next;
    Node tmpExtra = head; 
    head=tmp;
    head.prev=null;
    return tmpExtra.element
}

我的代码是否正确?我在想吗? 感谢

0 个答案:

没有答案