博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 25. Reverse Nodes in k-Group
阅读量:6413 次
发布时间:2019-06-23

本文共 2923 字,大约阅读时间需要 9 分钟。

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

你可能感兴趣的文章
10K入职linux运维岗位小伙伴感谢信及面试经历分享
查看>>
zookeeper入门之Curator的使用之几种监听器的使用
查看>>
[转]Reporting Service部署之访问权限
查看>>
innerxml and outerxml
查看>>
validform校验框架不显示错误提示
查看>>
flink 获取上传的Jar源码
查看>>
Spring Data JPA Batch Insertion
查看>>
UEditor自动调节宽度
查看>>
JAVA做验证码图片(转自CSDN)
查看>>
Delphi TServerSocket,TClientSocket实现传送文件代码
查看>>
JS无聊之作
查看>>
Mac上搭建ELK
查看>>
443 Chapter7.Planning for High Availability in the Enterprise
查看>>
框架和语言的作用
查看>>
unidac连接ORACLE免装客户端驱动
查看>>
Cygwin + OpenSSH FOR Windows的安装配置
查看>>
咏南中间件支持手机客户端
查看>>
fastscript增加三方控件之二
查看>>
Windows Vista RTM 你准备好了么?
查看>>
Tensorflow Serving 模型部署和服务
查看>>