链表旋转-Java

题目:链表旋转

Description

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2

输出: 4->5->1->2->3->NULL

解释:

向右旋转 1 步: 5->1->2->3->4->NULL

向右旋转 2 步: 4->5->1->2->3->NULL

示例 2:

输入: 0->1->2->NULL, k = 4

输出: 2->0->1->NULL

解释:

向右旋转 1 步: 2->0->1->NULL

向右旋转 2 步: 1->2->0->NULL

向右旋转 3 步: 0->1->2->NULL

向右旋转 4 步: 2->0->1->NULL

Input

输入包含两行,第一行为 2 个整数 n, kn,k (1 ≤ k ≤ n≤ 100000)用空格隔开,第二行为 n 个整数,用空格隔开,表示链表每一个节点的值

Output

输出单独的一行,即链表旋转后的结果,用空格隔开

Sample Input 1 

5 2
1 2 3 4 5

Sample Output 1

4 5 1 2 3

Sample Input 2 

3 4
0 1 2

Sample Output 2

2 0 1

思路分析

1. 先遍历一边链表,标记尾节点,计算出链表的总长度n;

2. 用k值跟n作比较。情况1:若k>=n则说明链表会平移至少一轮,取余后变为第二种情况;情况2:若k<n则说明平移不足一轮,k值保持不变;

3. 通过n-k-1计算出移动链表后的头节点位置的前一个节点,令尾节点的next指向头节点,head指向移动后的头节点,最后再令新头节点的前一个节点的next为null。

Java代码

//链表节点
class linkNode{
    int value;
    linkNode next;

    public linkNode(){}

    public linkNode(int value, linkNode next) {
        this.value = value;
        this.next = next;
    }
}
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int value = sc.nextInt();
        linkNode head = new linkNode(value, null);
        linkNode q = head;
        //创建链表
        for (int i = 1; i < n; i++) {
            int temp = sc.nextInt();
            linkNode node = new linkNode(temp, null);
            q.next = node;
            q = q.next;
        }

        //旋转链表
        linkNode result = rotateLink(head, k);
        //输出旋转后的结果
        for (linkNode p = result; p != null; p = p.next) {
            System.out.print(p.value + " ");
        }
    }

    public static linkNode rotateLink(linkNode head, int k){
        int n;
        linkNode p = head;
        if (head == null){
            return null;
        }
        //遍历链表,求链表的长度
        for (n = 1; p.next != null; p = p.next) {
            n++;
        }
        //对k进行判断,如果k>n取余,即链表旋转一周不变
        k %= n;
        linkNode q = head;
        //令q指向旋转后的头节点
        for (int i = 0; i != n-k-1 ; i++) {
            q = q.next;
        }
        p.next = head;
        head = q.next;
        q.next = null;
        return head;
    }
}

上述代码能够正常运行,但对于数据量较大的情况在oj上提交会提示Time Limit Exceeded,问题在于Scanner读入数据较慢,可以用BufferedReader进行改进。

BufferedReader

改模板可以直接背下来用来代替所有的Scanner

class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /** call thReader method to initialize reader for InputStream */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }

    /** get next word */
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine() );
        }
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}

使用该工具类的时候需要先进行初始化

Reader.init(System.in);
int n = Reader.nextInt();
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值