题目


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. [原创]InnoDB体系结构

    参阅:<innodb存储引擎内幕> innodb整体的体系结构如下图所示:  整体结构分两大部分:内存和进程其中内存包括:buffer_pool\redo log buffer\addit ...

  2. 学web前端开发写给新手的建议,超实用!

    01 前面的话 如今我们使用的互联网,客户端与服务器端的交互无时无刻不在发生.比如我们在浏览器打开网页,浏览器就是客户端,将网页数据发过来的也就是服务器.其实服务器,并没有什么特别的,也就是一台昼夜不 ...

  3. Tomcat 笔记-设置虚拟主机

    通过作用虚拟主机,可以使多个不同域名的网站共存于一个Tomcat中 在tomcat的server.xml文件中添加主机名: <Host name="hostname" app ...

  4. WPF ListBox 一些小知识点

    页面代码: <Grid Grid.Row="0" Grid.Column="2"> <ListBox x:Name="lvStep& ...

  5. 在vmware 中使用桥连接 连接到网络

    vMware虚拟机以后,连不上网,通过ifconfig命令,查看结果,如图所示: 然后,我想尝试一下,在虚拟机中ping 本地物理机地址,结果如图. 总结起来,主要有4步: 1.使用chkconfig ...

  6. Pdf文件处理组件对比(Aspose.Pdf,Spire.Pdf,iText7)

    目的 因为公司是做医疗相关软件的,所以经常和文档打交道,其中就包含了Pdf.医院的Pdf(通常是他们的报告)都千奇百怪,而我们一直以来都是在用一些免费且可能已经没人维护了的组件来处理Pdf,所以就经常 ...

  7. Java基础总结--Java编程环境变量配置

    1.jdk--bin--都是命令行程序(图形化是对命令行的封装)eg javac&java执行javac必须切换到其所在目录--太麻烦---想在任意目录下使用要执行一个命令--先在当前目录下找 ...

  8. aapt不是内部命令

    解决方法:在E:\sdk\build-tools\目录下的任意文件夹下查找aapt,复制到E:\sdk\platform-tools,具体盘符是情况而定,如果还不行,尝试配置环境变量!

  9. 使用css3实现瀑布流布局效果

    使用CSS3可以轻松实现瀑布流布局,但这种方法有个缺点,就是对于商城类型的网站,价格筛选时,并不能达到理想效果. 1.column-count 把div中的文本分为多少列 2.column-width ...

  10. grunt-contrib-connect自动刷新html页面

    grunt-contrib-connect可以在我们开发的时候自动刷新页面,省去了手动刷新的时间. 下面说一下如何配置grunt-contrib-connect 1.下载插件包 npm install ...