Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.
For example,
Given this linked list: 1->2->3->4->5
For k = 2, you should return: 2->1->4->3->5
For k = 3, you should return: 3->2->1->4->5
先计算长度,再分组,每组中node翻转,翻转用交换收尾方法
package pack;
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
int len = lenListNode(

该博客介绍了如何针对给定的链表,按k个节点一组进行翻转操作。例如,对于链表1->2->3->4->5,当k=2时,返回的翻转链表应为2->1->4->3->5;当k=3时,返回3->2->1->4->5。博主提出了先计算链表长度,然后分组,对每组内部节点使用交换首尾的方法进行翻转的策略。
订阅专栏 解锁全文
852

被折叠的 条评论
为什么被折叠?



