package 链表操作.q25_k个一组翻转链表;
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
/**
* 难点在于返回每个部分被修改的头节点,新建一个头节点的前置节点 o(n)
*/
public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode hair = new ListNode(0);
hair.next = head;
ListNode pre = hair;
ListNode end = hair;
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 hair.next;
}
private ListNode reverse(ListNode head) {
ListNode pre = null;
ListNode curr = head;
while (curr != null) {
ListNode next = curr.next;
curr.next = pre;
pre = curr;
curr = next;
}
return pre;
}
}