算法题练习系列之(十八): 反转链表

--------------------------------------------------------------------------------------------------------------------------------------------------------

时间限制:1秒  空间限制:32768K  代码长度限制 100 KB

--------------------------------------------------------------------------------------------------------------------------------------------------------

题目描述

给定一个常数K以及一个单链表L,请编写程序将L中每K个结点反转。例如:给定L为1→2→3→4→5→6,K为3,
则输出应该为3→2→1→6→5→4;如果K为4,则输出应该为4→3→2→1→5→6,即最后不到K个元素不反转。
输入描述:
每个输入包含1个测试用例。每个测试用例第1行给出第1个结点的地址、结点总个数正整数N(<=105)、以及
正整数K(<=N),即要求反转的子链结点的个数。结点的地址是5位非负整数,NULL地址用-1表示。
接下来有N行,每行格式为:
Address Data Next
其中Address是结点地址,Data是该结点保存的整数数据,Next是下一结点的地址。
输出描述:
对每个测试用例,顺序输出反转后的链表,其上每个结点占一行,格式与输入相同。
输入例子:
00100 6 4
00000 4 99999
00100 1 12309
68237 6 -1
33218 3 00000
99999 5 68237
12309 2 33218
输出例子:
00000 4 33218
33218 3 12309
12309 2 00100
00100 1 99999
99999 5 68237
68237 6 -1

--------------------------------------------------------------------------------------------------------------------------------------------------------

实现思路:

(1).创建表示节点的对象Node,将输入的节点用map存储,key为节点地址,value为节点;
(2).创建一个list来存储节点集合,通过第一个节点地址,在map中找到对应的节点node,将节点加入list中;
(3).根据node的next地址,从map中找到对应的节点,并加入到list中,循环该过程,直到节点next不合法为止;
(4).通过list长度和K,确定反转链表需要执行的次数leapTime,循环处理list,根据K及起始节点位置,将各个区段的节点顺序倒序处理;
(5).由于节点位置变换,节点在list中对应的next地址不是下一个节点的address,循环遍历list,逐一处理节点的next地址,指向下一个节点的address;
(6).逐一打印list中节点元素信息即可。

--------------------------------------------------------------------------------------------------------------------------------------------------------


package com.biyao.algorithm.niuke.a1;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class Main_a1_015 {
    
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            String first = scan.next();
            int n = scan.nextInt();
            int k = scan.nextInt();
            Map<String,Node> nodeMap = new HashMap<String,Node>();
            for (int i = 0; i < n; i++) {
                String address = scan.next();
                int value = scan.nextInt();
                String next = scan.next();
                nodeMap.put(address, new Node(address,value,next));
            }
            List<Node> nodeList = new ArrayList<Node>();
            nodeList.add(0, nodeMap.get(first));
            for (int i = 0; i < Integer.MAX_VALUE; i++) {
                if(nodeMap.containsKey(nodeList.get(i).next)){
                    nodeList.add(i+1, nodeMap.get(nodeList.get(i).next));
                }else{
                    break;
                }
            }
            
            int len = nodeList.size();
            int leapTime = len/k;
            int start = 0,end = Math.min(start + k, len);
            for (int i = 0; i < leapTime; i++) {
                int left = start,right = end-1;
                for (int j = start; j < end; j++) {
                    if(left >= right){
                        break;
                    }
                    Node node = nodeList.get(left);
                    nodeList.set(left, nodeList.get(right));
                    nodeList.set(right, node);
                    left++;
                    right--;
                }
                start = start + k;
                end = Math.min(start + k, len);
            }
              
            for (int i = 0; i < len-1; i++) {
                if(nodeList.get(i) == null){
                    break;
                }
                if(nodeList.get(i+1) != null){
                    nodeList.get(i).next = nodeList.get(i+1).address;
                }
            }
            for (int i = 0; i < nodeList.size(); i++) {
                System.out.println(nodeList.get(i).address + " " + nodeList.get(i).value + " " + nodeList.get(i).next);
            }
        }
    }
      
    private static String formatNum(int num){
        if(num >= 0 && num <= 9){
            return "0000" + num;
        }else if(num >= 10 && num <= 99){
            return "000" + num;
        }else if(num >= 100 && num <= 999){
            return "00" + num;
        }else if(num >= 1000 && num <= 9999){
            return "0" + num;
        }
        return num + "";
    }
  
}
  
class Node{
    String address;
    int value;
    String next;
    public Node(String address,int value,String next){
        this.address = address;
        this.value = value;
        this.next = next;
    }
}

  但该程序性能有问题,提交总是超时,暂时没找到可通过的方式。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值