Loading [MathJax]/jax/input/TeX/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >LintCode 旋转链表题目分析代码

LintCode 旋转链表题目分析代码

作者头像
desperate633
发布于 2018-08-22 04:28:57
发布于 2018-08-22 04:28:57
27800
代码可运行
举报
文章被收录于专栏:desperate633desperate633
运行总次数:0
代码可运行

题目

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

样例 给出链表1->2->3->4->5->null和k=2 返回4->5->1->2->3->null

分析

关键是找到第一段和第二段的点,分割开来,最后再合在一起就行了,设置两个指针,假设第一段的长度是x,那么x+n会等于链表的长度,所以方法自然出来了类似于寻找倒数第n个节点。

代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
private int getLength(ListNode head) {
        int length = 0;
        while (head != null) {
            length ++;
            head = head.next;
        }
        return length;
    }
    
    public ListNode rotateRight(ListNode head, int n) {
        if (head == null) {
            return null;
        }
        
        int length = getLength(head);
        n = n % length;
        
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        head = dummy;
        
        ListNode tail = dummy;
        for (int i = 0; i < n; i++) {
            head = head.next;
        }
        
        while (head.next != null) {
            tail = tail.next;
            head = head.next;
        }
        
        head.next = dummy.next;
        dummy.next = tail.next;
        tail.next = null;
        return dummy.next;
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017.03.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
LeetCode 24. Swap Nodes in Pairs题目分析代码
题目 给一个链表,两两交换其中的节点,然后返回交换后的链表。 样例 给出 1->2->3->4, 你应该返回的链表是 2->1->4->3。 分析 由于没有头结点不好操作 那就自己加一个头结点 代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ pu
desperate633
2018/08/22
3720
​LeetCode刷题实战61:旋转链表
https://leetcode-cn.com/problems/rotate-list/
程序员小猿
2021/01/20
2780
​LeetCode刷题实战61:旋转链表
K 个一组翻转链表(递归,Kotlin)
你的算法只能使用常数的额外空间。 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
一个会写诗的程序员
2020/05/08
5220
LinkedList - 61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative.
ppxai
2020/09/23
4660
打卡群刷题总结0715——旋转链表
链接:https://leetcode-cn.com/problems/rotate-list
木又AI帮
2020/07/17
2380
61. Rotate List(Linked List-Medium)
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given
Jack_Cui
2018/01/08
5360
LeetCode 25. Reverse Nodes in k-Group分析代码
我们设计一个翻转k的算法 head -> n1 -> n2 ... nk -> nk+1 => head -> nk -> nk-1 .. n1 -> nk+1 return n1 那么我们只要接着对这个方法传入n1即可,重复调用这个方法,直到遇到null
desperate633
2018/08/22
3010
【Leetcode】61.旋转链表
昨晚吃火锅吃撑了回来这道题,还算顺利~~ 链表的题目,其实就是在考指针交换,这个题目先让链表连成一个环,然后再切开就可以完成了。
Leetcode名企之路
2018/09/12
5410
【Leetcode】61.旋转链表
LintCode 交换链表当中两个节点题目分析代码
给你一个链表以及两个权值v1和v2,交换链表中权值为v1和v2的这两个节点。保证链表中节点权值各不相同,如果没有找到对应节点,那么什么也不用做。
desperate633
2018/08/22
2800
61. 旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 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->
张伦聪zhangluncong
2022/10/26
3200
golang刷leetcode 链表(3)反转链表
给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前。
golangLeetcode
2022/08/02
2790
【leetcode刷题】T101-旋转链表
我们假设链表的长度是length,那么k>=length时,相当于旋转k%length次。
木又AI帮
2019/07/17
3970
LeetCode每日一题-6:k个一组翻转链表
你的算法只能使用常数的额外空间。 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
墨明棋妙27
2022/09/23
3280
LintCode 重排链表题目分析代码
样例 给出链表 1->2->3->4->null,重新排列后为1->4->2->3->null。
desperate633
2018/08/22
2710
LinkedList - 92. Reverse Linked List II
Reverse a linked list from position m to n. Do it in one-pass.
ppxai
2020/09/23
3360
LeetCode 19. Remove Nth Node From End of List题目分析代码
样例 给出链表1->2->3->4->5->null和 n = 2. 删除倒数第二个节点之后,这个链表将变成1->2->3->5->null.
desperate633
2018/08/22
3300
每天一道leetcode61-旋转链表
昨天的题解 题目 每天一道leetcode61. 旋转链表 分类:双指针 中文链接: https://leetcode-cn.com/problems/rotate-list/description/ 英文链接 https://leetcode.com/problems/rotate-list/description/ 题目详述 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4->5-
乔戈里
2019/09/17
5260
golang刷leetcode 链表(1)交换旋转
一、给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。
golangLeetcode
2022/08/02
3220
LeetCode Remove Linked List Elements
Remove all elements from a linked list of integers that have value val. Example*****Given:* 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 Credits:Special thanks to @mithmatt for adding this problem and creating all test cases.
desperate633
2018/08/22
3900
LeetCode Remove Linked List Elements
一文搞懂《链表反转》
翻转链表一直都是热门题目,笔者就在某大型互联网公司的面试题中碰到过这种题目,这种题目很常常见,相对应的变形和扩展也很多,今天我们就来攻克它吧。
lucifer210
2019/09/25
1.2K0
一文搞懂《链表反转》
相关推荐
LeetCode 24. Swap Nodes in Pairs题目分析代码
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验