02-线性结构3 Reversing Linked List

传送门

02-线性结构3 Reversing Linked List(25 分)

题目

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 (≤10​^5​​ ) 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 33218
Sample Output:

00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

提交代码

#include <iostream>
#include <cstdlib>
#include <cstdio>
#define MAX 100000

using namespace std;

typedef struct _node {
    int num;
    int next;
} Node;

Node a[MAX];
Node* reverse(Node *p, int k);

int main(int argc, char const * argv[])
{
    int head, n, k;
    cin >> head >> n >> k;
    int add;
    for ( int i=0; i<n; i++ ) {
        cin >> add;
        cin >> a[add].num >> a[add].next;
    }
    Node nodep;
    nodep.next = head;
    head = reverse(&nodep,k)->next;
    int flag = 1;
    do {
        printf("%05d %d ", head, a[head].num);
        if ( a[head].next == -1 ) {
            printf("-1");
            flag = 0;
        }
        else printf("%05d\n", a[head].next);
        head = a[head].next;
    } while ( flag );
    return 0;
}

Node* reverse(Node *p, int k)
{
    int now, old, temp;
    now = p->next;
    old = a[now].next;
    if ( old == -1 ) return p;
    temp= a[old].next;
    if ( temp== -1 ) {
        if ( k==2 ) {
            a[old].next = now;
            a[now].next = -1;
            p->next = old;
            return p;
        }
        else return p;
    }
    bool f = 0;
    for ( int cnt=1; cnt<k; cnt++ ) {
        if ( cnt==k-1 && a[old].next==-1 ) f=1;
        a[old].next = now;
        now = old;
        old = temp;
        temp= a[temp].next;
    }
    Node rep = {0, p->next};
    if ( f ) a[p->next].next = -1;
    else a[p->next].next = old;
    
    p->next = now;
    
    if ( f ) return p;
    
    int cnt = 0;
    temp = old;
    
    while ( a[old].next!=-1 ) {
        old = a[old].next;
        cnt++;
        if ( cnt==k-1 ) break;
    }
    if ( cnt==k-1 ) {
        //p.next = temp;
        reverse(&a[rep.next], k);
    }
    return p;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 百龄醉鲜竹酒选用5-10年优质茅台53度原浆酒和2岁以上优质竹子,再经活竹竹腔内经过1-3年时间的自然酿造和吸收天...
    黔西刘光兵阅读 703评论 0 0
  • 太安逸的生活总是缺乏原动力, 必须 时不时的让生活给记榔头 才会想着前行或者改变 作为老师 曾经不舍的人里面也存在...
    腊月腊日阅读 162评论 0 0
  • 今日作业 1、颜色或颜色搭配勾到我的什么? 2、若对发型、彩妆品牌选择时,抽人像卡;对款式无法抉择时,抽伴侣卡。 ...
    Bigcherry阅读 565评论 0 0
  • 信任+依赖=信赖,如果不能依赖,总会有爱这个中间变量让你:信任+爱=依赖,依赖+信任=信赖。 1 现在是北京时间2...
    墨临风阅读 329评论 0 1