![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/141d008abee9c792447c42db1a7c8b03.png)
解题思路
- 首先把一个链表分开,一块k个数
其中定义的结点包括 pre(这k个结点之前的结点),next(之后的),start、end - 把这k结点内部进行反转
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode node = new ListNode(0);
node.next = head;
ListNode pre = node;
ListNode end = node;
while(end.next!=null){
for(int i=0;i<k&&end!=null;i++) end = end.next;
if(end==null) break;
ListNode start = pre.next;
ListNode next = end.next;
end.next = null;
pre.next = reverse(start);
start.next = next;
pre = start;
end = pre;
}
return node.next;
}
public ListNode reverse(ListNode head){
ListNode pre = null;
ListNode cur = head;
while(cur!=null){
ListNode next = cur.next;
cur.next = pre;
pre=cur;
cur = next;
}
return pre;
}
}