题目


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

基本思路

使用一个长度为99999的数组存放数据和next指针。最后进行反转。有两种方法:1.另建一个数组,存放要输出的数据,每次遍历反转个数个元素,并按相反顺序填入数组;2.把每个元素的next指针指向上一个元素,最后把原头结点的next指向新头结点的旧next。

代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

struct Node
{
    int val;
    int next;
};

int reverseList(Node a[],int header,int reverse);
int getLength(Node a[],int header);
int main()
{
    Node a[100000];
    int header, length, reverse=0;
    cin >> header;
    cin >> length;
    cin >> reverse;

    for (int i = 0; i < length; i++)
    {
        int pos;
        scanf("%d", &pos);
        scanf("%d %d", &a[pos].val, &a[pos].next);
    }

    int realLength=getLength(a, header);
    //反转
    int nowPos=header,nowHeader=0,newHeader=header,lastRear=0;
    if(reverse>1)
    {
        for(int i=0;i<realLength/reverse;i++)
        {
            //第一次反转更新头结点
            if(nowPos==header)
            {
                lastRear=nowPos;
                nowHeader=reverseList(a,header,reverse);
                newHeader=nowHeader;
                nowPos=a[lastRear].next;
            }
            else
            {
                nowHeader=reverseList(a,nowPos,reverse);
                a[lastRear].next=nowHeader;
                lastRear=nowPos;
                nowPos=a[lastRear].next;
            }
        }
    }

    //输出
    int pos=newHeader;
    while (pos!=-1)
    {
        printf("%05d %d ", pos, a[pos].val);
        if (a[pos].next!= -1)
            printf("%05d\n", a[pos].next);
        else
            printf("%d\n", a[pos].next);
        pos = a[pos].next;
    }
    return 0;
}

int getLength(Node a[],int header)
{
    int pos=header;
    int length=0;
    while (pos!=-1)
    {
        length++;
        pos = a[pos].next;
    }
    return length;
}

int reverseList(Node a[],int header,int reverse)
{
    int nowP=a[header].next,lastP=header;
    //先将第2-reverse间的元素的next指向上一个元素
    for(int i=0;i<reverse-1;i++)
    {
        if(nowP==-1)
            break;
        int newPos;
        newPos=a[nowP].next;
        a[nowP].next=lastP;
        //更新位置
        lastP=nowP;
        nowP=newPos;
    }
    a[header].next=nowP;
    header=lastP;
    return header;
}

总结

第一次做时还是没审清题意,题目是要每K个元素反转,一开始只反转了一次。还有就是注意调用反转函数次数应该是链表的真实长度/反转长度,因为可能会有多余结点。

02-线性结构3 Reversing Linked List的更多相关文章

  1. pat02-线性结构1. Reversing Linked List (25)

    02-线性结构1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, ...

  2. 02-线性结构3 Reversing Linked List(25 point(s)) 【链表】

    02-线性结构3 Reversing Linked List(25 point(s)) Given a constant K and a singly linked list L, you are s ...

  3. 02-线性结构3 Reversing Linked List

    02-线性结构3 Reversing Linked List   (25分) 时间限制:400ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 http ...

  4. 02-线性结构2 Reversing Linked List

    由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. Given a constant K and a singly linked list L, you are ...

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

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/664 5-2 Reversing Linked List   (25分) Given a ...

  6. 数据结构练习 02-线性结构2. Reversing Linked List (25)

    Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...

  7. 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 elem ...

  8. 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 elem ...

  9. PAT02-线性结构3 Reversing Linked List

    题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...

随机推荐

  1. Struts2 xml 详解

    <!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 --> <include file=" ...

  2. 制作Windows服务项目详细攻略

    1.在windows服务下面获得根目录: string assemblyFilePath = Assembly.GetExecutingAssembly().Location; string asse ...

  3. 符合语言习惯的Python优雅编程技巧

    Python最大的优点之一就是语法简洁,好的代码就像伪代码一样,干净.整洁.一目了然.要写出 Pythonic(优雅的.地道的.整洁的)代码,需要多看多学大牛们写的代码,github 上有很多非常优秀 ...

  4. mybatis 的mapper配置文件sql语句中, 有时用到 大于, 小于等等

    一, 用<![CDATA[   ]]>标识,例如: <if test="create_timeStart != null and create_timeStart != ' ...

  5. js判断是否使用的是微信浏览器

    代码如下: function is_weixin() { var ua = navigator.userAgent.toLowerCase(); return ua.match(/MicroMesse ...

  6. [C#]使用ILMerge将源DLL合并到目标EXE(.NET4.6.2)

    本文为原创文章,如转载,请在网页明显位置标明原文名称.作者及网址,谢谢! 本文主要是使用微软的ILMerge工具将源DLL合并到目标EXE,因此,需要下载以下工具: https://www.micro ...

  7. input file选择图片后 预览

    很多前端都选择用插件来实现图片预览,这个小功能也可以很简单的用jQuery来实现 简单的jQuery实现input file选择图片后,可以预览图片的效果 简单的HTML代码: <div> ...

  8. Knight Moves

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  9. Unity 游戏框架搭建 (七) 减少加班利器-QApp类

    本来这周想介绍一些框架中自认为比较好用的小工具的,但是发现很多小工具都依赖一个类----App. App类的职责: 1.接收Unity的生命周期事件. 2.做为游戏的入口. 3.一些框架级别的组件初始 ...

  10. visual studio 2013使用github获取代码

    如图点击"视图""团队资源管理器".   点击主页上方的那个插头形状按钮"连接到团队项目".点击"克隆"   然后在克隆 ...