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 33218
Sample 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;}

0 0