题目


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. DES加密And解密

    加密分为:单向加密,双向加密和对称加密. 单向加密:通过对数据进行摘要计算生成密文,密文不可逆推还原.算法代表:Base64,MD5,SHA; 双向加密:与单向加密相反,可以把密文逆推还原成明文,双向 ...

  2. Android SDK国内更新

    恩,废话就不多说了,俩网址,mirrors.neusoft.edu.cn和ubuntu.buct.edu.cn,在这里感谢wecloud和北京化工大学,十分感谢提供方便之门

  3. 异步任务--celery发送邮件

    安装两个python包: pip install celery==3.1.25 pip install django-celery==3.1.17 在配置文件settings.py中做如下配置: a) ...

  4. 保存单文件为mhtml

    1.F6 ,在地址栏输入 输入:chrome://flags 然后ctrl+f: 输入:mhtml 启动,重启即可保存为单文件.

  5. mysql 外键的几种约束

    restrict方式 同no action, 都是立即检查外键约束    --限制,指的是如果字表引用父表的某个字段的值,那么不允许直接删除父表的该值: cascade方式 在父表上update/de ...

  6. LeetCode 643. Maximum Average Subarray I (最大平均值子数组之一)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  7. LeetCode 34. Search for a Range (找到一个范围)

    Given an array of integers sorted in ascending order, find the starting and ending position of a giv ...

  8. Java常用类(四)之数组工具类Arrays

    前言 数组的工具类java.util.Arrays 由于数组对象本身并没有什么方法可以供我们调用,但API中提供了一个工具类Arrays供我们使用,从而可以对数据对象进行一些基本的操作. 一.Arra ...

  9. centos7 防火墙 配置

    1.查看Firewall 服务状态 systemctl status firewalld 2.查看Firewall 的状态 firewall-cmd --state 注意: firewalld默认配置 ...

  10. git笔记------自己学习git的心得

    git个人学习总结: git是一个管理代码的版本控制系统,用git init创建一个git可以管理的仓库,这个仓库里有一个工作区,我们最基本的那些命令操作都是在工作区完成,在创建仓库的时候,在工作区里 ...