简体   繁体   English

firstLast Java链接列表/节点

[英]firstLast Java Linked list/nodes

I'm confused on how each node gets linked to another and how to make sure that if I want the first node to be linked in after the one at the end, I'm not running an infinite loop. 我对每个节点如何链接到另一个节点感到困惑,以及如何确保如果我希望第一个节点在最后一个节点之后链接,我没有运行无限循环。 For example, in this problem.. 例如,在这个问题..

Write a method firstLast that could be added to the LinkedIntList class that moves the first element of the list to the back end of the list. 编写一个firstLast方法,可以将其添加到LinkedIntList类中,该类将列表的第一个元素移动到列表的后端。 Suppose a LinkedIntList variable named list stores the following elements from front (left) to back (right): 假设名为list的LinkedIntList变量存储从前(左)到后(右)的以下元素:

[18, 4, 27, 9, 54, 5, 63] If you made the call of list.firstLast();, the list would then store the elements in this order: [18,4,27,9,54,5,63]如果您调用了list.firstLast();,那么列表将按以下顺序存储元素:

[4, 27, 9, 54, 5, 63, 18] If the list is empty or has just one element, its contents should not be modified. [4,27,9,65,5,63,18]如果列表为空或只有一个元素,则不应修改其内容。

My first attempt is to do this..but to no avail: 我的第一次尝试就是这样做......但无济于事:

`public void firstLast(){
    ListNode temp = front;//stores/references the first node
    temp.next = null;//ensures that first node isn't referring to any other 
//node
    ListNode current = front;//reaches end node/first one with a null 
//reference
    while(current.next != null){
        current = current.next;
    }
    front.next = temp;//links end node to first node
    front = current.next;////now that skips the first element`

but the output is [18] -> [18] (cycles!) . 但输出是[18] -> [18] (cycles!) Please advise 请指教

The function firstLast() can be coded as follows: 函数firstLast()可以编码如下:

public void firstLast()
{
    ListNode temp = removeFirst();
    appendLast( temp );
}

So, now you have broken the problem down into two smaller problems. 所以,现在你把问题分解为两个较小的问题。 This tends to be a very good strategy to solving any kind of problem. 这往往是解决任何问题的非常好的策略。

The point you need to remember, in order to implement your removeFirst() is that you have to modify front to point to the 2nd node, which is front.next . 您需要记住的一点是,为了实现您的removeFirst() ,您必须修改front指向第二个节点,即front.next

I'm confused on how each node gets linked to another 我对每个节点如何链接到另一个节点感到困惑

In a single-linked list, every node knows only the next one. 在单链表中,每个节点只知道下一个节点。 So you need to keep track of the first, commonly called head (in your implementation it's "front"), because it's essential to access all the elements. 所以你需要跟踪第一个,通常称为head (在你的实现中它是“前面”),因为访问所有元素是必不可少的。

... and how to make sure that if I want the first node to be linked in after the one at the end, I'm not running an infinite loop. ...以及如何确保如果我希望第一个节点在最后一个节点之后链接,我没有运行无限循环。

The last node has no next node after it, so it's next pointer is null . 最后一个节点后面没有下一个节点,因此它的next指针为null You can use this condition to avoid infinite loops. 您可以使用此条件来避免无限循环。 (Just make sure the last node has next correctly as null .) (只需确保最后一个节点next正确为null 。)

Your implementation could be fixed as: 您的实施可以修复为:

public void firstLast() {
    // list is empty or has single element -> nothing to do
    if (front == null || front.next == null) {
        return;
    }

    // save the first
    ListNode first = front;

    // move the head
    front = front.next;

    // find the end
    ListNode current = front;
    while (current.next != null) {
        current = current.next;
    }

    // append the node
    current.next = first;

    // reset the next pointer of the new last node
    first.next = null;
}

Given you implemented: 鉴于您实施:

void push(ListItem item) {
    end.next = item;
    item.next = null;
}

and

ListItem pop() {
    ListItem first = front;
    front = front.next;
    return first;
}

your method becomes: 你的方法变成:

void firstLast() {
    push(pop());
}

Note : untested code with no checks for null s or list size. 注意 :未经测试的代码,不检查空值或列表大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM