题目 分析 判断是否为k长链表单节点处理k长链表反转链接前后链表 代码 import java.util.*;/** public class ListNode {* int val;* ListNode next = null;* }*/public class Solution {/**** @param head ListNode类* @param k int整型* @return ListNode类*/public ListNode reverseKGroup (ListNode head, int k) {if (head == null || head.next == null || !isKn(head, k)) {return head;}ListNode start = head;int i = k;ListNode pre = null;ListNode temp = null;// write code herewhile (k > 0 ) {k--;temp = head.next;head.next = pre;pre = head;head = temp;if (k == 0 && head != null) {start.next = reverseKGroup(head, i);}if (head == null) {break;}}return pre;}/*** 检查是否k倍数*/public boolean isKn(ListNode head, int k) {ListNode knCheck = head;int kn = k;while (knCheck != null && kn > 0) {knCheck = knCheck.next;kn--;}if (kn != 0) {return false;}return true;} }