LeetCode 25. Reverse Nodes in k-Group

Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

You may not alter the values in the nodes, only nodes itself may be changed.

Only constant memory is allowed.

For example,
Given this linked list: 1->2->3->4->5

For k = 2, you should return: 2->1->4->3->5

For k = 3, you should return: 3->2->1->4->5

 

分两个步骤来解决问题, 因为这里对内存使用的要求比较严格, 所以先要解决在不创建新的链表的前提下,如何将单向链表反转;

其次再根据k值, 如何分段反转。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <cstdio>
#include <iostream>
#include "time.h"
#include<stdlib.h>
#include<sstream>
#include<string>
using namespace std;
 
/*
     25. Reverse Nodes in k-Group
*/
 
class ListNode
{
public :
     int val;
     ListNode* next;
     ListNode( int val2){
         val=val2;
         next=NULL;
     }
};
 
class Solution {
public :
     ListNode* reverse(ListNode* head) {
         ListNode* cur; //µ±Ç°ÕýÔÚ´¦ÀíµÄ½Úµã
         ListNode* head_old=head; //×ʼµÄÍ·½áµã
         ListNode* head_front= new ListNode(-1); //×Ô¼º´´½¨µÄÍ·½áµãÇ°ÃæµÄ½Úµã
         head_front->next=head;
         if (head==NULL) return NULL;
         while (head_old->next!=NULL){
             cur=head_old->next;
             head_old->next=cur->next;
             cur->next=head_front->next;
             head_front->next=cur;
         }
         return head_front->next;
     }
 
     ListNode* reverseKGroup(ListNode* head, int k) {
         ListNode* head_front= new ListNode(-1);
         head_front->next=head;
         ListNode* cur=head_front;
         ListNode* first=NULL;
         ListNode* first_new=NULL;
         ListNode* last=NULL;
         ListNode* last_new=NULL;
         ListNode* last_old=head_front;
         ListNode* next_record;
         
         int count=0;
         int first_turn=1;
         
         //cal the num of nodes
         int nums=0;
         ListNode* tmp=head;
         while (tmp){
             nums++;
             tmp=tmp->next;
         }
         
         //special case
         if (head==NULL) return NULL;
         if (k==1) return head;
         if (k==nums) return reverse(head);
         
         //ordinary case
         while (cur->next!=NULL){
             cur=cur->next;
             count++;
             if (count==1)first=cur;
             else if (count==k)
             {
                 last=cur;
                 next_record=last->next; //record next element
                 last->next=NULL; //conveninent to use reverse()
                 first_new=reverse(first);
                 last_new=first;
                 last_new->next=next_record;
                 last_old->next=first_new;
                 last_old=last_new;
                 count=0;cur=last_new;
                 if (first_turn==1){
                     first_turn=0;
                     head=first_new;
                 }
             }
             
             
         }
         return head;
     }
};
 
int main()
{
     ListNode* head = new ListNode(1);
     ListNode* l1 = new ListNode(2);
     ListNode* l2 = new ListNode(3);
     ListNode* l3 = new ListNode(4);
     ListNode* l4 = new ListNode(5);
     head->next=l1;
     l1->next=l2;
     l2->next=l3;
     l3->next=l4;
     Solution s;
     head=s.reverseKGroup(head,3);
     ListNode* cur=head;
     while (cur!=NULL){
         cout<<cur->val<< " " ;
         cur=cur->next;  
     }
     getchar ();
     return 0;
}

 





转载于:https://www.cnblogs.com/gremount/p/5810190.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值