PAT 数据结构 02-线性结构1. Reversing Linked List (25)
来源:互联网 发布:a股人工智能概念股 编辑:程序博客网 时间:2024/06/06 00:07
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L. For example, given L being 1→2→3→4→5→6, if K = 3, then you must output 3→2→1→6→5→4; if K = 4, you must output 4→3→2→1→5→6.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (<= 105) which is the total number of nodes, and a positive K (<=N) which is the length of the sublist to be reversed. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.
Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.
Sample Input:00100 6 400000 4 9999900100 1 1230968237 6 -133218 3 0000099999 5 6823712309 2 33218Sample Output:
00000 4 3321833218 3 1230912309 2 0010000100 1 9999999999 5 6823768237 6 -1
静态链表,注意是每K个结点翻转。
/*2015.7.8*/#include <iostream>#include <vector>#include <stack>#include <string>#include <sstream>#include <iomanip>#include <math.h>using namespace std;struct LNode{int val;int next;};int main(){int head,N,K;scanf("%d%d%d",&head,&N,&K);vector<LNode> LList(100000);int a,b,c;for(int i=0;i<N;i++){scanf("%d%d%d",&a,&b,&c);LList[a].val=b;LList[a].next=c;}int pp=head;int count=0;while(pp!=-1){count++;pp=LList[pp].next;}int dummy=head;int pre=head;int cur=LList[pre].next;for(int i=1;i<K;i++){if(cur==-1)break;LList[pre].next=LList[cur].next;LList[cur].next=dummy;dummy=cur;cur=LList[pre].next;}int seg=count/K;int tail;int dummy1;if(seg>=2){while(--seg){tail=pre;dummy1=cur;pre=cur;cur=LList[pre].next;for(int i=1;i<K;i++){LList[pre].next=LList[cur].next;LList[cur].next=dummy1;dummy1=cur;cur=LList[pre].next;}LList[tail].next=dummy1;}}int p=dummy;while(LList[p].next!=-1){printf("%05d %d %05d\n",p,LList[p].val,LList[p].next);p=LList[p].next;}printf("%05d %d -1\n",p,LList[p].val);return 0;}
2015年8月15日在LeetCode上遇到一样的题,还是用链表写来的畅快。/*2015.8.15cyq*/#include <iostream>#include <string>#include <vector>using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}ListNode(const vector<int> &a):val(a[0]),next(NULL){ListNode *prev=this;for(int i=1;i<a.size();i++){prev->next=new ListNode(a[i]);prev=prev->next;}}};class Solution {public: ListNode* reverseKGroup(ListNode* head, int k) {if(k==1||head==nullptr||head->next==nullptr)return head;int listLen=0;ListNode *p=head;while(p){listLen++;p=p->next;}int reverseTime=listLen/k;//需要翻转的有几段ListNode dummy(-1);dummy.next=head;ListNode* HH=&dummy;//翻转段的前一个结点while(reverseTime--){ListNode* prev=HH->next;ListNode* cur=prev->next;for(int i=2;i<=k;i++){prev->next=cur->next;cur->next=HH->next;HH->next=cur;cur=prev->next;}HH=prev;}return dummy.next; }};int main(){int a[]={1,2,3,4,5};vector<int> ivec(a,a+sizeof(a)/sizeof(int));Solution solu;ListNode *p=&ListNode(ivec);p=solu.reverseKGroup(p,2);while(p){cout<<p->val<<" ";p=p->next;}return 0;}
- PAT 数据结构 02-线性结构1. Reversing Linked List (25)
- 数据结构 PAT 02-线性结构2 Reversing Linked List 单链表实现
- PAT数据结构_02-线性结构3 Reversing Linked List (25分)
- PAT 02-线性结构3 Reversing Linked List 【JAVA实现】
- 02-线性结构1. Reversing Linked List (25)
- 02-线性结构1. Reversing Linked List (25)
- 02-线性结构1. Reversing Linked List (25)
- 02-线性结构1. Reversing Linked List (25)
- [数据结构]02-线性结构3 Reversing Linked List
- PAT 02-1. Reversing Linked List (25)
- 02-线性结构2 Reversing Linked List
- 02-线性结构2 Reversing Linked List
- 02-线性结构2 Reversing Linked List
- 02-线性结构2 Reversing Linked List
- 02-线性结构2 Reversing Linked List
- 02-线性结构3 Reversing Linked List
- 02-线性结构2 Reversing Linked List
- 02-线性结构3 Reversing Linked List
- 黑马程序员_异常体系
- 悟空学Linux专栏----第25篇
- UITableView显示不全
- JAVA反射学习之——基本学习
- Linux内核中ioremap映射的透彻理解
- PAT 数据结构 02-线性结构1. Reversing Linked List (25)
- Android 广播的发送与接收
- Xcode6模拟器路径
- 怎样把多个ppt转换成pdf最有效率
- Badboy 参数化读取excel
- 静下心,沉住气
- CRC校验C实现
- 获取手机的短信内容和联系人信息
- HYSBZ 1013: [JSOI2008]球形空间产生器sphere(高斯消元啊 模板)