《leetCode》:Reverse Nodes in k-Group
2015-11-07 22:16
537 查看
题目
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个节点为一组翻转,并将返回的头结点保存。最后将返回的所有头结点串起来就可以了。
实现代码如下:
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ /* 思路:先写一个关于长度为k的链表翻转,然后利用递归将其求解 */ /* 参数的说明: @param head:链表的头结点 @param n:链表中结点的个数 返回值:返回链表的头结点 */ #include<stdio.h> #include<stdlib.h> struct ListNode { int val; struct ListNode *next; }; struct ListNode *reverseList(struct ListNode *head,int n){ if(head==NULL||n<1){ return NULL; } struct ListNode *print=head; while(print!=NULL){ printf("翻转前%d ",print->val); print=print->next; } struct ListNode *pre=NULL; struct ListNode *cur=head; struct ListNode *next=NULL; for(int i=0;i<n-1;i++){ next=cur->next;//保存下一个结点指针 cur->next=pre; pre=cur; cur=next;// } //for循环结束时,cur指向原链表的最后一个结点。即新链表的头结点 cur->next=pre; struct ListNode *printNode=cur; while(printNode!=NULL){ printf("翻转后%d ",printNode->val); printNode=printNode->next; } return cur; } struct ListNode* reverseKGroup(struct ListNode* head, int k) { if(head==NULL||k<0){ return NULL; } //先求链表的长度 struct ListNode *temp=head; int len=0; while(temp!=NULL){ len++; temp=temp->next; } if(k>len){//当k大于链表长度时,返回原链表头结点 return head; } if(k==1){//不翻转,直接返回 return head; } //开始按k个借点为一组来进行翻转 struct ListNode *cur=head; struct ListNode *pre=NULL; struct ListNode *curHead=head; struct ListNode *nextHead=NULL; struct ListNode **allHead=(struct ListNode **)malloc((len/k)*sizeof(struct ListNode *)); if(allHead==NULL){ exit(EXIT_FAILURE); } int index=0; int count=len/k; for(int i=0;i<count;i++){ //以k个结点为段,保存下一个即将要翻转的头结点 for(int i=0;i<k;i++){ pre=cur; cur=cur->next;//下一轮的头结点 } //先将头一段翻转 pre->next=NULL; allHead[index]=reverseList(curHead,k); index++; //问题再这里:为什么没有变呢??? curHead=cur; }//此for循环结束的时候,curHead指向最后没有翻转的剩余结点的头结点 //最后将这所有链表拼接起来就ok了 struct ListNode *newListHead=allHead[0]; struct ListNode *node=allHead[0]; for(int i=0;i<index;i++){ for(int j=0;j<k-1;j++){//使node指向最后一个结点 node=node->next; } if(i!=index-1){ node->next=allHead[i+1]; node=node->next;//使的结点指向后一段的头结点 } else{ node->next=curHead; } } return newListHead; } int main(void){ //创建一个链表 ListNode *node1=(ListNode *)malloc(sizeof(ListNode)); ListNode *node2=(ListNode *)malloc(sizeof(ListNode)); ListNode *node3=(ListNode *)malloc(sizeof(ListNode)); ListNode *node4=(ListNode *)malloc(sizeof(ListNode)); ListNode *node5=(ListNode *)malloc(sizeof(ListNode)); node1->val=8; node1->next=node2; node2->val=7; node2->next=node3; node3->next=node4; node3->val=3; node4->next=node5; node4->val=5; node5->next=NULL; node5->val=18; ListNode *head=reverseKGroup(node1,2); printf("\n"); while(head!=NULL){ printf("%d ",head->val); head=head->next; } // ListNode *head=(ListNode *)malloc(sizeof(ListNode)); return 0; }
AC结果如下:
思路很简单,但是一些细节问题,花了很长的时间。
相关文章推荐
- f2fs解析(七)node管理器中的 free_nid 结构体
- f2fs源码解析(五) node管理结构梳理
- wordpress搬家到 linode 步骤简析
- Nodejs简单的初始安装、部署Window版
- 配置webstorm使用supervisor时出现 /usr/bin/env: node: 没有那个文件或目录 解决方法
- [LeetCode]Populating Next Right Pointers in Each Node
- [LeetCode]Populating Next Right Pointers in Each Node
- Populating Next Right Pointers in Each Node II 解答
- webstorm开发nodejs的技巧--代码提示DefinitelyTyped
- node.js 获取req 参数的三种方式
- [KAFKA-1724]Errors after reboot in single node setup
- Node.js中文版API文档
- Node.js Error: Cannot find module 'extend', 在CC-A80 Ubuntu linaro上运行Node.js 程序报错
- OSX El Capitan node-5.0.0下sudo npm install遇到的权限问题
- Cubieboard CC-A80性能之Ubuntu Linaro编译安装Node.js
- node.js安装模块报 .node-gyp\4.2.1\Release\node.lib : fatal error LNK1106: invalid file or disk full:
- Hadoop根据SecondaryNameNode恢复Namenode
- 【Leetcode】之Remove Nth Node From End of List
- Nodejs+Grunt配置SASS项目自动编译
- 通读cheerio API ——NodeJs中的jquery