网站建设 技术团队介绍,wordpress 设置访客登陆,中山门户网站制作在哪里买,扬中seo给出一个链表#xff0c;每 k 个节点一组进行翻转#xff0c;并返回翻转后的链表。
k 是一个正整数#xff0c;它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍#xff0c;那么将最后剩余节点保持原有顺序。
示例 :
给定这个链表#xff1a;1-2-3-每 k 个节点一组进行翻转并返回翻转后的链表。
k 是一个正整数它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍那么将最后剩余节点保持原有顺序。
示例 :
给定这个链表1-2-3-4-5
当 k 2 时应当返回: 2-1-4-3-5
当 k 3 时应当返回: 3-2-1-4-5
public class ReverseNodesInkGroup {public static void main(String[] args) {// 1 - 2 - 3 - 4 - 5ListNode node1 new ListNode(1);ListNode node2 new ListNode(2);ListNode node3 new ListNode(3);ListNode node4 new ListNode(4);ListNode node5 new ListNode(5);node1.next node2;node2.next node3;node3.next node4;node4.next node5;ListNode reverseKGroup reverseKGroup(node1, 3);System.out.println();while (reverseKGroup ! null) {System.out.println(reverseKGroup.val);reverseKGroup reverseKGroup.next;}}public static ListNode reverseKGroup(ListNode head, int k) {ListNode currentNode head;int count 0;while (currentNode ! null count ! k) { // 每k个节点一组分隔currentNode currentNode.next;count;}if (count k) {currentNode reverseKGroup(currentNode, k);/// 递归的解决子问题while (count-- 0) { // 将头节点从链表中切掉放在下一组链表的前端切除k次即可将链表翻转ListNode temp head.next; // 保存该组链表的第二个节点head.next currentNode; // head节点的下一位指向currentNode(第一次循环时是下一组链表的头节点之后每截取一次就往前移)currentNode head; // currentNode节点前移到headhead temp; // head节点重新指向该组的第一个节点开始下次循环}head currentNode; //最终该段的所有节点将会截空head应指向currentNode}return head;}}