PAT002 Reversing Linked List

题目:

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 (<= 105) 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

分析:主要考查链表逆序。 如果通过数组进行排序,然后每K个逆序输出,对于多余的节点将无法通过测试,所以实现必须使用链表。代码:
typedef struct reverseNode {

    long address;
    int value;
    long nextAdd;
    struct reverseNode *next;

} ReverseNode;

ReverseNode *reverseLink(ReverseNode *head, int reverseLen)
{
    ReverseNode *new = head->next;
    ReverseNode *old = new->next;
    int count = 1;
    while (count < reverseLen) {
        ReverseNode *temp = old->next;
        old->next = new;
        old->nextAdd = new->address;
        new = old;
        old = temp;
        count++;
    }
    head->next->next = old;
    if (!old) {
        head->next->nextAdd = -1;
    } else {
        head->next->nextAdd = old->address;
    }
    ReverseNode *temp = head->next;
    head->next = new;
    head->nextAdd = new->address;
    return temp;
}

int main()
{
    // 读取输入
    long beginAddress;
    int number, reverseLen;
    scanf("%ld %d %d", &beginAddress, &number, &reverseLen);

    ReverseNode *head = (ReverseNode *)malloc(sizeof(ReverseNode));
    ReverseNode *a[number];

    for (int i = 0; i < number; i++) {
        long address, nextAdd;
        int value;
        scanf("%ld %d %ld",&address, &value, &nextAdd);
        ReverseNode *node = (ReverseNode *)malloc(sizeof(ReverseNode));
        node->address = address;
        node->value = value;
        node->nextAdd = nextAdd;
        node->next = 0;
        a[i] = node;
        if (beginAddress == address) {
            head->next = node;
        }
    }

    // 对输入数据通过链表连接起来
    ReverseNode *temp = head->next;
    int actualNumber = 1;
    int recyCount = number;
    while (temp->nextAdd != -1 && recyCount-- > 0) {
        for (int i = 0; i < number; i++) {
            ReverseNode *tempNode = a[i];
            if (tempNode->address == temp->nextAdd) {
                temp->next = tempNode;
                temp->nextAdd = tempNode->address;
                temp = temp->next;
                actualNumber++;
                break;
            }
        }
    }

    // 反转
    if (reverseLen > 1) {
        int reverseCount = actualNumber / reverseLen; // 需要进行反转的次数
        ReverseNode *tempHead = head;
        while (reverseCount-- > 0) {
            tempHead = reverseLink(tempHead, reverseLen);
        }
    }

    ReverseNode *ptr = head->next;
    while (ptr) {
        if (ptr->nextAdd == -1) {
            printf("%.5ld %d -1\n", ptr->address, ptr->value);
        } else {
            printf("%.5ld %d %.5ld\n", ptr->address, ptr->value, ptr->nextAdd);
        }
        ptr = ptr->next;
    }
}

运行结果:

[测试点5,是使用超级大量数据,由于我在将输入数据通过链表连接起来那一步采用的是嵌套循环,复杂度O(N^2),可能是这里导致的,不确定。 但是总体反转思路是这样的,后面找到原因了再进行更新]

时间: 2024-10-19 10:23:07

PAT002 Reversing Linked List的相关文章

Reversing Linked List

题目来源:PTA02-线性结构2 Reversing Linked List   (25分) Question: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; i

PAT1074 Reversing Linked List (25)详细题解

02-1. Reversing Linked List (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 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,

[PAT] 02-线性结构2 Reversing Linked List(单向链表的逆转) - C语言实现

今天突然想起自己的cnblog有差不多一年没更了??放一道很久前做的也写好了很久但是一直忘记发布的题.如果有不同的算法欢迎分享~ [PAT]02-线性结构2 Reversing Linked List   (25分) Given a constant KK and a singly linked list LL, you are supposed to reverse the links of every KK elements on LL. For example, given LL bein

PAT 1074. 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 Specifica

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

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

02-线性结构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 Kelements 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 outp

PAT 1074 Reversing Linked List[链表][一般]

1074 Reversing Linked List (25)(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 mus

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

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 ou

02-线性结构2 Reversing Linked List

由于最近学的是线性结构,且因数组需开辟的空间太大.因此这里用的是纯链表实现的这个链表翻转. 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,