题解 | #链表中的节点每k个一组翻转#
链表中的节点每k个一组翻转
https://www.nowcoder.com/practice/b49c3dc907814e9bbfa8437c251b028e
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
class ListNode {
int val;
ListNode next;
public ListNode(int val) {
this.val = val;
}
public ListNode(ListNode next) {
this.next = next;
}
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
@Override
public String toString() {
return "ListNode{" +
"val=" + val +
", next=" + next +
'}';
}
}
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param head ListNode类
* @param m int整型
* @param n int整型
* @return ListNode类
*/
public static void main(String[] args) {
ListNode r = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
// ListNode r = new ListNode(5, null);
Solution s = new Solution();
System.out.println("++++++++ result +++++++");
System.out.println(s.reverseKGroup(r, 3));
// 创建链表
}
public static ListNode findEndNode(ListNode h) {
if (h == null) {
return null;
}
while (h.next != null) {
h = h.next;
}
return h;
}
public static int nodeLength(ListNode h) {
if (h == null) {
return 0;
}
int result = 0;
while (h.next != null) {
result++;
h = h.next;
}
result++;
return result;
}
public ListNode reverseKGroup(ListNode head, int k) {
if (Solution.nodeLength(head) < k) {
return head;
}
ListNode nextNode = Solution.findIndexNode(head, k + 1);
ListNode rest = reverseKGroup(nextNode, k);
ListNode start = head;
ListNode end = Solution.findEndNode(head);
while (head.next != null) {
k--;
head = head.next;
// 找完了相应的长度
if (k == 1) {
break;
}
}
// write code here
if (k == 1) {
//刚好找完 且满足长度
head.next = null;
ListNode h = Solution.reverseListNode(start);
start.next = rest;
return h;
} else {
//长度不够
return start;
}
}
public static ListNode findIndexNode(ListNode head, int n) {
if (head == null || n == 0) {
return null;
}
ListNode result = null;
int index = 0;
while (true) {
index++;
if (index < n ) {
result = head;
head = head.next;
if (head==null) {
result = null;
break;
}
continue;
}
if (index == n) {
result = head;
break;
}
if (head == null) {
result = null;
break;
}
}
return result;
}
// public static ListNode subListNode(ListNode head, int m, int n) {
// if (head == null) {
// return null;
// }
// // 至少一个节点
// int index = 0;
// ListNode sub = null;
// while (true) {
// // 记录到了第index个节点
// index++;
// // 未到头节点
// if (index < m) {
// head = head.next;
// continue;
// }
// // 找到头节点
// if (index == m) {
// // 记录头节点
// sub = head;
// continue;
// }
//
// // 在区间里面
// if (index > m && index <= n) {
// // 移动指针
// head = head.next;
// continue;
// }
// // 找完了
// if (head == null) {
// break;
// }
// if (index > n) {
// head.next = null;
// break;
// }
// }
// return sub;
// }
public static ListNode reverseListNode(ListNode head) {
if (head.next == null) {
return head;
}
ListNode rest = reverseListNode(head.next);
head.next.next = head;
head.next = null;
return rest;
}
}
查看14道真题和解析