2-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

理解题意:

  • 输入的第一行很重要:三个数分别代表:首元素的地址,元素总个数N,前K个元素做逆序
    接下来就是N行数据输入,三个数分别代表,该元素地址,该元素,该元素指向的下个节点的地址
  • 输出就很容易理解了

我的答案:

// C++版本,更简单一些

#include<iostream>
#include<stdio.h>
#include<algorithm>    ///使用到reverse 翻转函数
using namespace std;

#define MAXSIZE 1000010   ///最大为五位数的地址

struct node    ///使用顺序表存储data和下一地址next
{
    int data;
    int next;
}node[MAXSIZE];

int List[MAXSIZE];   ///存储可以连接上的顺序表
int main()
{
    int First, n, k;
    cin>>First>>n>>k;   ///输入头地址 和 n,k;
    int Address,Data,Next;
    for(int i=0;i<n;i++)
    {
        cin>>Address>>Data>>Next;
        node[Address].data=Data;
        node[Address].next=Next;
    }

    int j=0;  ///j用来存储能够首尾相连的节点数
    int p=First;   ///p指示当前结点
    while(p!=-1)
    {
        List[j++]=p;
        p=node[p].next;
    }
    int i=0;
    while(i+k<=j)   ///每k个节点做一次翻转
    {
        reverse(&List[i],&List[i+k]);
        i=i+k;
    }
    for(i=0;i<j-1;i++)
        printf("%05d %d %05d\n",List[i],node[List[i]].data,List[i+1]);
    printf("%05d %d -1\n",List[i],node[List[i]].data);
    return 0;
}

c版本:

// C 实现版本, 与老师讲的一致

#include <stdio.h>
#include <stdlib.h>
#define MaxSize 100000
typedef int Ptr;
//结构数组模拟链表
struct node{
    int key;
    Ptr next;
}list[MaxSize];

//逆转某一段含K个结点的链表
Ptr Reverse ( Ptr head, int K ) {
    Ptr New, Old, Tmp; //New指向逆转链表表头,Old指向未逆转表头,Tmp记录Old后一个结点
    int cnt = 1;
    New = head;
    Old = list[New].next;
    while ( cnt < K ) {
        Tmp = list[Old].next; //记录Old下一个结点,防止逆转指针后丢失
        list[Old].next = New; //逆转指针
        New = Old; Old = Tmp; //New,Old向后转移一个
        cnt++;
    }
    list[head].next = Old; //原来的第一个结点逆转后变为最后一个结点,并连接剩下未逆转的元素
    return New;
}

//判断是否需要逆转
int NeedReverse ( Ptr head , int K) {
    int i;
    for( i = 1; list[head].next != -1; head = list[head].next ) {
        i++;
        if ( i == K ) return 1; //还有K个或以上结点,需要逆转
    }
    return 0; //不足K个结点,不需要逆转
}

//逆转整个链表
Ptr ReversingLinkedList( Ptr head , int K ) {
    Ptr UnreversedHead = head;  //未逆转的链表的第一个结点
    Ptr ListHead; //整个表的第一个结点
    Ptr TempTail; //临时表尾,用来连接下一段逆转的链表

    if ( NeedReverse( UnreversedHead, K ) ) { //第一次先判断是否需要逆转
        ListHead = Reverse( UnreversedHead, K ); //记住逆转后的整个链表的第一个结点
        TempTail = UnreversedHead; //记录此逆转链表的表尾
        UnreversedHead = list[TempTail].next; //记录未逆转链表的表头
    }
    else //链表结点个数小于K,无需逆转
        return head;

    while ( NeedReverse( UnreversedHead, K ) ) {
        list[TempTail].next = Reverse( UnreversedHead, K ); //上一个逆转链表的表尾与这个逆转链表的表头连接
        TempTail = UnreversedHead;
        UnreversedHead = list[TempTail].next;
    }
    return ListHead;
}

//输出链表
void PrintLinkedList ( Ptr head ) {
    Ptr temp = head;
    for(; list[temp].next !=  -1;  temp = list[temp].next)
        printf("%05d %d %05d\n", temp, list[temp].key, list[temp].next);
    printf("%05d %d %d\n", temp, list[temp].key, list[temp].next);
}

int main(){
    Ptr ad, head;
    int N, K;
    scanf("%d %d %d", &head, &N, &K);
    for(int i = 0; i < N; i++){
        scanf("%d", &ad);
        scanf("%d %d", &list[ad].key, &list[ad].next);
    }
    PrintLinkedList( ReversingLinkedList( head, K ) );
    return 0;
}

答案解析

明天再补上!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,546评论 6 507
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,224评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,911评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,737评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,753评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,598评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,338评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,249评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,696评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,888评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,013评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,731评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,348评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,929评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,048评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,203评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,960评论 2 355

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,332评论 0 10
  • 什么是理想? 前几天和一位兄长聊天天,讲到了理想与现实的话题;他认为:理想只是空想,并没有现实意义、理想很丰满,现...
    罗志琦阅读 1,102评论 4 17
  • 周日,L先生和我联系。因为很久没有见面了,彼此一开始就是客套的问候。我问他现在在做什么。他说在东五环五方桥一个德云...
    惠茹姐姐阅读 2,701评论 0 1
  • 一个人只有真正战胜了自己,才能真正的拥有自己!才能做真实的自己,才能接受自己的不完美,才能珍惜自己所拥有的东西.
    感悟生命_82b6阅读 136评论 0 0