把链表的头部移到尾部 [英] Moving head of linked list to tail

查看:185
本文介绍了把链表的头部移到尾部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Java中编写一个方法,将链表中的第一个元素移动到最后一个位置。

I need to write a method in Java that moves the first element in a linked list to the last position.

为了实现这一目标,我相信我已经拥有设置节点以引用head之后的第一个元素,然后将下一个节点设置为null。我尝试使用我的方法执行此操作,但在运行该方法时,输出不正确。

To accomplish this, I believe I'd have to set a node to reference the first element after the head, then set the next node to null. I tried to do this with my method, but when running the method, the output is incorrect.

我所拥有的其他类很可能太大而无法在此处发布,但我想我只需要帮助概念化如何将第一个元素移动到列表的末尾。

The rest of the class I have is most likely too large to post here, but I think I only need help on conceptualizing how to move the first element to the end of the list.

我写的方法是:

public void moveFirstToEnd() {
    if (head.next == null) {
        throw new NoSuchElementException();
    } 

    Node node = head.next;
    node.next = null;
    head.next = node;
    tail.next = node;
    tail = node;
}


推荐答案

你想要删除头部列表,并使其成为新的尾巴。你应该弄清楚如何做到这一点,代码将是一个逻辑表示。

You want to remove the head of the list and make it the new tail. You should work out how to do that in your head, and the code will be a logical representation of that.


  1. 删除头部名单。新的头部成为下一个项目。

  2. 现在,移除的项目是独立的;之后没有任何内容。

  3. 将节点放在列表的末尾。新尾部成为该节点。

您现在的代码,正如您所看到的,并不完全是这样做的。一步完成一步:

Your code now, as you can see, doesn't exactly do that. Working through one step at a time:

所以,第1步:

Node node = head;
head = head.next; // <- remove head, new head becomes next item

然后,第2步:

node.next = null; // there's nothing after it.

最后,第3步:

tail.next = node; // <- add to end of list
tail = node; // <- becomes new end of list

或者,如果您想要将其可视化:

Or, if you'd rather visualize it:

Node node = head:

+------+    +------+    +------+    +------+
| head |--->|      |--->|      |--->| tail |
+------+    +------+    +------+    +------+
  node

head = head.next:

+------+    +------+    +------+    +------+
|      |--->| head |--->|      |--->| tail |
+------+    +------+    +------+    +------+
  node

node.next = null:

+------+    +------+    +------+    +------+
|      |    | head |--->|      |--->| tail |
+------+    +------+    +------+    +------+
  node

tail.next = node:

            +------+    +------+    +------+    +------+
            | head |--->|      |--->| tail |--->|      |
            +------+    +------+    +------+    +------+
                                                  node

tail = node:

            +------+    +------+    +------+    +------+
            | head |--->|      |--->|      |--->| tail |
            +------+    +------+    +------+    +------+
                                                  node

顺便说一下,如果你碰巧有一个 popFront (或其他)和/或追加操作定义,不要忘记你也可以使用它们;无意义重复代码:

By the way, if you already happen to have a popFront (or whatever) and/or append operation defined, don't forget you can use those, too; no sense duplicating code:

Node node = popFront(); // if you have this
append(node); // if you have this

这篇关于把链表的头部移到尾部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆