reverse k group

1     ListNode *reverseKGroup(ListNode *head, int k) {
 2         // Start typing your C/C++ solution below
 3         // DO NOT write int main() function
 4         if( k<= 1 || head == NULL) return head;
 5         
 6         ListNode * root = new ListNode( -1 );
 7         root->next = head;
 8         ListNode* ptr1 = root;
 9         ListNode* ptr2 ;
10         ListNode* tmp, *tmp2,*tmp3;
11         
12         int count = 1;
13         while( head != NULL )
14         {
15             if( count < k ) {
16                 head = head -> next ;
17                 count ++;
18                 continue;          
19             }
20             tmp = head->next;
21             
22             head = ptr1 -> next;
23             ptr2 = head->next;
24             for( int i=1;i<k;i++)
25             {
26                 head -> next = ptr2-> next;
27                 ptr2->next = ptr1 -> next;
28                 ptr1 -> next = ptr2;
29                                
30                 ptr2 = head->next;
31                             
32             } 
33             ptr1 = head;
34             head = tmp;
35             count = 1;
36         }
37         return root->next;
38     }

原文链接: https://www.cnblogs.com/jumpinGGrass/p/3170099.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/94464

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午2:38
下一篇 2023年2月10日 上午2:38

相关推荐