简体   繁体   English

是否可以在循环中使用单个语句来反向链接列表?

[英]Is it possible to reverse a linked list with a single statement within a loop?

Assume we have 假设我们有

ListNode {
     ListNode next;
     int number;

     //gets sets
}

Is it possible to reverse the list using a loop with only one string whithin a loop? 是否可以使用仅在循环中包含一个字符串的循环来反转列表?

Something like this: 像这样:

for(...) {
// only one string of code (one statement)
}

One guy told me that it's possible but I don't understand how. 一个人告诉我,有可能,但我不知道如何。 Can you help with that please? 你能帮忙吗? Thanks! 谢谢!

Yes. 是。 It is possible. 有可能的。 It isn't pretty. 不好看

First, a toString method for ListNode to verify the answer. 首先,一个用于ListNode验证答案的toString方法。 You can print root and reverse(root) to see the results work. 您可以打印rootreverse(root)来查看结果。

public String toString() {
    if (next != null) {
        return "" + num + " " + next.toString();
    }
    return "" + num;
}

In-Place Reversal 就地逆转

A zero-line answer that reverses in place: 零线答案在原地反转:

static ListNode reverse(ListNode node) {
    ListNode headNode = null;
    ListNode previousNode = null;
    for (; node != null; previousNode = headNode, headNode = node, node = node.next, headNode.next = previousNode)
        ;
    return headNode;
}

Basically, a lot of node child swapping. 基本上,很多节点子交换。 We track the previous node so we can attach it to the head node, but we also have to track the next node for iteration. 我们跟踪前一个节点,以便可以将其附加到头节点,但是我们还必须跟踪下一个节点以进行迭代。

Return a new reversed List 返回新的反向列表

Now, the code golfy reverse method that returns a new set of Nodes in a reverse order. 现在,代码golfy reverse方法以相反的顺序返回一组新的Node。

static ListNode reverse(ListNode node) {
    ListNode headNode = null;
    ListNode followerNode = null;
    for (; node != null; node = node.next, followerNode = headNode) {
        (headNode = new ListNode(node.num)).next = followerNode;
    }
    return headNode;
}

Basically, these methods abuse many things about Java that you really shouldn't. 基本上,这些方法会滥用Java的许多您实际上不应该使用的东西。 We abuse the for loop by not using any integers for iteration. 我们通过不使用任何整数进行迭代来滥用for循环。 Then we abuse the increment section by including two lines of code for assignment. 然后,我们通过包含两行分配代码来滥用增量部分。

Then singular line of code sets the headNode to a new ListNode of the value, and then sets its next to the previous headNode . 然后,单行代码将headNode设置为该headNode的新ListNode ,然后将其next设置为先前的headNode

Practically speaking, it would take less effort to write it correctly than to write something that saves lines. 实际上,与编写节省行的内容相比,正确编写它要花费更少的精力。

I mean, technically, if you really wanted to, you could put everything into the for loop iterator portion and get no lines, but in the spirit of trying to actually follow the prompt, only true assignments have been placed in the for loop. 我的意思是,从技术上讲,如果您确实愿意,可以将所有内容放入for循环迭代器部分,而不会出现任何行,但是出于尝试实际遵循提示的精神,仅将真正的赋值放置在for循环中。

That being said, it is customary coding convention that we DO NOT stuff code into a for loop for execution to save a few lines. 话虽这么说,这是习惯的编码约定,我们不将代码塞入for循环中以便执行以节省几行。 In this day and age, the only real purpose for that is shortening down spacing in code golf. 在当今时代,这样做的唯一真正目的是缩短代码高尔夫的间隔。

PS: in the future, if anyone tells you something is possible in Java you should probably ask them to explain it. PS:将来,如果有人告诉您Java中有可能实现的目标,您可能应该请他们解释一下。 Whether or not things are possible in Java is a superset of whether we should be doing it in Java. 在Java中是否可能实现事情是我们是否应该在Java中完成事情的一个超集。

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

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