PAT02-1Reversing Linked List (25) 单链表逆序
02-1. Reversing Linked List (25)
http://www.patest.cn/contests/mooc-ds/02-1
ard 作者 CHEN, Yue
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 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218Sample Output:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1 题目大意:反转单链表,给定常数K和单链表L,要求按每K个节点反转单链表,如:L: 1->2->3->4->5->6 K=3,输出:3->2->1->6->5->4,如果K=4,输出:4->3->2->1->5->6. 输入说明:每次输入一个案例,对每个案例,第一行内容是链表第一个节点的地址,节点数N(N<=100,000)(不一定是最终形成的单链表的节点数),常数K(<=N),K是需要反转的子链表的长度,节点的地址是一个5位的非负整数,NULL用-1来代替。 下面输入N行 格式如下: Address Data Next Address代表节点的位置,Data是整型数字,Next是下一个节点的位置。 输出说明:输出反转后的单链表,每个节点占一行,格式和输入的一样。 样例输入:
00100 6 4 00000 4 99999 00100 1 12309 68237 6 -1 33218 3 00000 99999 5 68237 12309 2 33218 样例输出:
00000 4 33218 33218 3 12309 12309 2 00100 00100 1 99999 99999 5 68237 68237 6 -1
#include<iostream> #include<cstdio> using namespace std; const int Max=100009; class LNode { public: int addr; int nextAddr; int data; class LNode *next; }; LNode * Rev(LNode * head,int k); void printList(LNode *a); int main() { int num=0;//记录链表上一共有多少个节点 int firstAddr; cin>>firstAddr; int n,k; cin>>n>>k; int data[Max]; int next[Max]; LNode a[n+9]; a[0].nextAddr=firstAddr; int tem; for(int i=0;i<n;i++) { cin>>tem; cin>>data[tem]>>next[tem]; } //构建单链表 int Ti=1; while(1) { if(a[Ti-1].nextAddr==-1) { a[Ti-1].next=NULL; num=Ti-1; break; } a[Ti].addr=a[Ti-1].nextAddr; a[Ti].data=data[a[Ti].addr]; a[Ti].nextAddr=next[a[Ti].addr]; a[Ti-1].next=a+Ti; Ti++; } //接下来进行链表的翻转 LNode * Re=a; LNode * r=NULL; if(k<=num) { for(int i=0;i<(num/k);i++) { r=Rev(Re,k); //Rev传回来的是翻转的最后一个节点,比如1->2->3->4->5 翻转后成为了1<-2<-3<-4 1->5, //则返回的是4的指针 //将头结点指向4 Re->next=r; Re->nextAddr=r->addr; //然后根据这个翻转之后的第一个节点追溯的下一个需要翻转的节点头结点、。 int j=0; while(j<k) { Re=Re->next; j++; } } } printList(a); return 0; } LNode * Rev(LNode * head,int k) { //传入的head为头结点 LNode *newNode=head->next; LNode *oldNode=newNode->next; LNode *tem=NULL; int cont=1; while(cont<k) { tem=oldNode->next; oldNode->nextAddr=newNode->addr; oldNode->next=newNode; newNode=oldNode; oldNode=tem; cont++; } //将反转的链表的最后一个节点与接下来将要翻转的头结点相连接 head->next->next=oldNode; if(oldNode!=NULL) { //如果oldNode不是最后一个节点。 head->next->nextAddr=oldNode->addr; } else { head->next->nextAddr=-1; } return newNode; } void printList(LNode *a) { LNode *p = a; while (p->next != NULL){ p = p->next; if (p->nextAddr != -1 ){ //格式输出,%.5意味着如果一个整数不足5位,输出时前面补0 如:22,输出:00022 printf("%.5d %d %.5d\n", p->addr, p->data, p->nextAddr); }else{ //-1不需要以%.5格式输出 printf("%.5d %d %d\n", p->addr, p->data, p->nextAddr); } } }