简体   繁体   English

链表 - 以 K 为一组反转链表

[英]Linked List - Reverse a Linked List in groups of K

I was following this Java solution to understand the given problem我正在关注这个 Java 解决方案来理解给定的问题

I got most of it, the reversal logic, the recursion part but under the main method, I don't get why we are assigning the head value to the value returned by the reverse method.我得到了大部分,反转逻辑,递归部分,但在 main 方法下,我不明白为什么我们将 head 值分配给 reverse 方法返回的值。

llist.head = llist.reverse(llist.head, 3); 

In llist.reverse(llist.head, 3);在 llist.reverse(llist.head, 3); it will reverse the head list with specified length(3) and assigning back to llist.head.它将反转具有指定长度(3)的头部列表并将其分配回 llist.head。

1->2->3->4->5->6->7->8->NULL, K = 3 1->2->3->4->5->6->7->8->NULL,K = 3

After first iteration第一次迭代后

3->2->1 ->4->5->6->7->8->NULL - it reverse the first 3 elements and in second iteration 3->2->1 ->4->5->6->7->8->NULL - 它反转前 3 个元素并在第二次迭代中

3->2->1-> 6->5->4 ->7->8->NULL and in last iteration 3->2->1->6->5->4->8->7->NULL 3->2->1-> 6->5->4 ->7->8->NULL 和在最后一次迭代中 3->2->1->6->5->4->8-> 7->空

finally llist.head holds the 3->2->1->6->5->4->8->7->NULL最后 llist.head 持有 3->2->1->6->5->4->8->7->NULL

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

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