题目


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. 如何设置App的启动图

    如何设置App的启动图,也就是Launch Image? Step1 1.点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image" 2.如 ...

  2. Spring bean 生命周期验证

    一.从源码注释看bean生命周期 从JDK源码上看,BeanFactory实现类需要支持Bean的完整生命周期,完整的初始化方法及其标准顺序(格式:接口 方法)为: 1.BeanNameAware s ...

  3. [mysql使用(2)] mysql的一些语法与Oracle的差别

    一.表空间 mysql的表空间有共享表空间和独占表空间,独占表空间,其实就是一张表一个表空间,其实也就是一张表一个数据文件,共享表空间似乎有点类似oracle的表空间,不同的表可以保存在同一个数据文件 ...

  4. Xilinx ISE 14.1生成Rom内核并读取Rom中的数据

    <一>建立一个项目readDataFromRom 详细过程参照另一篇文章 http://www.cnblogs.com/LCCRNblog/p/3397666.html <二> ...

  5. ZOJ 3777-Problem Arrangement(状压DP)

    B - Problem Arrangement Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  6. Problem Q

    Problem Description A factory produces products packed in square packets of the same height h and of ...

  7. 2015-2016 ACM-ICPC, NEERC, Southern Subregional Contest A Email Aliases(模拟STL vector+map)

    Email AliasesCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:524288KB     ...

  8. 【经验分享】Trachtenberg system(特拉亨伯格速算系统)

    二战期间,俄国的数学家Jakow Trachtenberg(1888-1953)被关进纳粹集中营,在狱中,他开发出了一套心算算法,这套算法后来被命名为Trachtenberg(特拉亨伯格)速算系统. ...

  9. cookie在不同域名domain、path下的读取规则

    cookie 子域名可以读父域名中的cookie 如在 .ping.com域下注入cookie,则该子域下的网页如p1.ping.com.p2.ping.com 都能读取到cookie信息 path的 ...

  10. mongodb集群【】

    参考 http://www.jianshu.com/p/2825a66d6aed http://www.cnblogs.com/huangxincheng/archive/2012/03/07/238 ...