/*** @Author:田宇寒.* @Date:Created in 14:12 2021年6月7日* @Description:K个一组翻转链表* @ModifiedBy:* @Version: 1.0*/public class code25 {public class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) {this.val = val;}ListNode(int val, ListNode next) {this.val = val;this.next = next;}}public ListNode reverseKGroup(ListNode head, int k) {/*** create by: 田宇寒* description: 时间复杂度:O(n) 空间复杂度O(1)* create time: 10:29 2021年6月9日* @Param: head* @Param: k* @return code25.ListNode*/ListNode hair = new ListNode(0);hair.next = head;ListNode pre = hair;while (head != null) {ListNode tail = pre;for (int i = 0; i < k; i++) {tail = tail.next;if (tail == null) {return hair.next;}}ListNode next = tail.next;ListNode[] listNodes = reverseNodeList(head, tail);head = listNodes[0];tail = listNodes[1];pre.next = head;tail.next = next;pre = tail;head = tail.next;}return hair.next;}private ListNode[] reverseNodeList(ListNode head, ListNode tail) {ListNode prev = tail.next;ListNode p = head;while (prev != tail) {ListNode nex = p.next;p.next = prev;prev = p;p = nex;}return new ListNode[] {tail, head};}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。