标签:str direct return dir pen pac odi package end
package leetcode_50; /*** * * @author pengfei_zheng * list划分为k组并且逆置 */ public class Solution25 { public class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public ListNode reverseKGroup(ListNode head, int k) { ListNode curr = head; int count = 0; while (curr != null && count != k) { // find the k+1 node curr = curr.next; count++; } if (count == k) { // if k+1 node is found curr = reverseKGroup(curr, k); // reverse list with k+1 node as head // head - head-pointer to direct part, // curr - head-pointer to reversed part; while (count-- > 0) { // reverse current k-group: ListNode tmp = head.next; // tmp - next head in direct part head.next = curr; // preappending "direct" head to the reversed list curr = head; // move head of reversed part to a new node head = tmp; // move "direct" head to the next node in direct part } head = curr; } return head; } }
LeetCode 25 Reverse Nodes in k-Group Add to List 划分list
标签:str direct return dir pen pac odi package end
原文地址:http://www.cnblogs.com/zpfbuaa/p/6527370.html