1074 Reversing Linked List(附测试点6分析)

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

 

思路

1、单链表逆转

 

复制代码
List Reverse(List L){
    List l=NULL;
    while(L!=NULL){
        List temp=L->Next;
        L->Next=l;
        l=L;
        L=temp;
    }
    return l;
}
复制代码

 

 解释:l为逆序后的链表的头指针,每次取正序链表的头元素,将逆序链表l插入到其后面,形成新的逆序链表,不断迭代,直至正序链表为空。

 

2、测试点6报段错误:

因为有些节点不在头节点指向的链表中,不能算到其中。因此总的结点个数不一定为n。

 

 

 

代码

复制代码
#include<stdio.h>
#include<iostream>
#include<map>
using namespace std;
struct Node{
    string address;
    int data;
    Node *next;
};
int main(){
    map<string, string> m1;
    map<string, int> m2;
    int n,k,num = 1;
    string start;
    cin>>start>>n>>k;
    for(int i=0; i<n; i++){
        string address,next;
        int data;
        cin>>address>>data>>next;
        m1[address] = next;
        m2[address] = data;
    }
    
    Node *head, *pre, *p;
    head = new Node;
    head->address = start;
    head->next = NULL;
    head->data = m2[start];
    string temp = m1[start];
    pre = head;
    
    while(temp != "-1"){
        p = new Node;
        p->address = temp;
        p->data = m2[temp];
        p->next = NULL;
        pre->next = p;
        pre = p;
        temp = m1[temp];
        num++;
    }
    
    
    Node *l = NULL;
    for(int i=0; i<num/k; i++){
        int j = k;
        while(j--){
            Node *temp = head->next;
            head->next = l;
            l = head;
            head = temp;
        }
        if(i > 0){
            cout<<l->address<<endl;
        }
        while(l != NULL){
            cout<<l->address<<" "<<l->data<<" ";
            if(l->next != NULL){
                cout<<l->next->address<<endl;
                l = l->next;
            }else{
                l = l->next;
                break;
            }
        }
    }
    if(head != NULL){
        cout<<head->address<<endl;
    }else{
        cout<<"-1"<<endl;
    }
    while(head != NULL){
        cout<<head->address<<" "<<head->data<<" ";
        if(head->next != NULL){
            cout<<head->next->address<<endl;
        }else{
            cout<<"-1"<<endl;
        }
        head = head->next;
    }
}
复制代码

 

posted @   Yohoc  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示