文章标题

本文介绍了一种算法,该算法将给定的单链表每K个元素进行一次反转,并提供了完整的实现代码,包括链表的建立、输入及反转输出。

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


下面开始分析题目:
1.题目意思是将链表每k个进行反转,不足k个的不反转,最后讲反转的链表输出。
难点如下:
2.首先判链表是否存在,如果输入的首地址等于-1,那么链表肯定不存在,程序直接return 0即可。
3.如果k值大于真实链表的大小,那么不需要进行反转,直接输出即可。
4.如果k值小于或者等于真实链表的大小,那么对反转的次数和每次反转的开始结束下标进行控制即可。
技术实现
5.链表的建立,我是用了哈希+vector,避免多次循环。用空间换取时间。
6.链表的输入,我采取了偷懒政策,我再交换节点的时候并没有真得对节点的next进行重新赋值,而是直接在输出的时候输出下一个节点的头地址。


代码如下,代码中有备注,还算清晰

#include <iostream>
#include <stdio.h>
#include <vector>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
struct node{//节点结构体 
    int add;
    int value;
    int next;
};
int main(int argc, char *argv[]) {
    int first,n,k;
    scanf("%d%d%d",&first,&n,&k);
    if(first == -1){//链表为空,什么也不输出 
        return 0;
    }
    node list[100010];//用于哈希,为了建表 
    for(int i=0;i<n;i++){
        int add,value,next;
        scanf("%d%d%d",&add,&value,&next);
        list[add].add = add;
        list[add].value = value;
        list[add].next=next;
    }
    vector<node> reallist;//存储真实的链表 
    while(first!=-1){
        int next = list[first].next;
        reallist.push_back(list[first]);
        first = next;
    }
//  printf("%d",reallist.size());
    int realsize = reallist.size();
    vector<node> result;//结果链表 
        int start = 0;
        int end = 0;
    if(realsize<k){//k值比真实的值大,因此不需要反转 
        for(int i=0;i<realsize-1;i++){
            printf("%05d %d %05d\n",reallist[i].add,reallist[i].value,reallist[i+1].add);//输出的技巧 
        }
        printf("%05d %d %d\n",reallist[realsize-1].add,reallist[realsize-1].value,-1);//输出最后一个 
    }
    else{
        int count = realsize/k;//可以反转的次数 
        start = 0;//第一次反转的开始位置 
        end = start + k-1;//第一次反转的结束位置 
        int index = 0;
        for(int i=1;i<=count;i++){//可以反转的次数 
            for(int j=end;j>=start;j--){//每次反转的循环 
                node t = reallist[j];
                result.push_back(t);
            }
            start += k;//反转的开始 
            end = start+k-1;//反转的结束 
        }
        for(int i = count*k;i<realsize;i++){//不能反转的 
            result.push_back(reallist[i]);
        }
    }
    for(int i=0;i<result.size()-1;i++){//输出 
        printf("%05d %d %05d\n",result[i].add,result[i].value,result[i+1].add);
    }
    printf("%05d %d %d\n",result[realsize-1].add,result[realsize-1].value,-1);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值