#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
static struct ListNode* reverseKGroup(struct ListNode* head, int k)
{
int len = 0;
struct ListNode dummy;
struct ListNode *prev = &dummy;
dummy.next = head;
for (; head != NULL; head = head->next) {
if (++len % k == 0) {
/* p always the original first one */
struct ListNode *p = prev->next;
/* loop condition implicits the final state */
while (prev->next != head) {
/* the new segment head */
struct ListNode *q = p->next;
/* deletion */
p->next = q->next;
/* insertion */
q->next = prev->next;
prev->next = q;
}
/* For iteration */
prev = p;
head = p;
}
}
return dummy.next;
}
int main(int argc, char **argv)
{
int i;
struct ListNode *p, *prev, dummy, *list;
dummy.next = NULL;
prev = &dummy;
for (i = 2; i < argc; i++) {
p = malloc(sizeof(*p));
int n = atoi(argv[i]);
printf("%d ", n);
p->val = n;
p->next = NULL;
prev->next = p;
prev = p;
}
putchar('\n');
list = reverseKGroup(dummy.next, atoi(argv[1]));
for (p = list; p != NULL; p = p->next) {
printf("%d ", p->val);
}
putchar('\n');
return 0;
}
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
DdddJMs__135
- 粉丝: 2444
- 资源: 586
最新资源
- cTrader指标:Variety Period clock control panel:品种周期时钟控制面板
- cTrader指标:Variety Period time switching panel:品种周期时间切换面板
- 字符串遗传算法-excited-JS-plus1S.zippython
- 附件文件下载安装啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
- sdram verilog 代码
- GNOME-Dia-Diagram-Editor-0.97.1-1-Win32-Zip-2010-02-07.tar.gz
- 80删除有序数组中的重复项 II.zip
- 79单词搜索.zip(算法)
- GNOME-Dia-Diagram-Editor-Shapes-Repository-20130624.tar.gz
- GNOME-Dia-Diagram-Editor-0.97.3-13.1-Linux(rpm)-2024-09-13.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)