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

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

问题描述

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

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

为了实现这一点,我相信我必须设置一个节点来引用头部之后的第一个元素,然后将下一个节点设置为空.我尝试用我的方法执行此操作,但是在运行该方法时,输出不正确.

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:

所以,第一步:

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(或其他)和/或 append 操作,请不要忘记您也可以使用它们;没有意义的重复代码:

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天全站免登陆