Rotate List 



Given a list, rotate the list to the right by k places, where k is non-negative.



For example:

Given 1->2->3->4->5->NULL and k = 2,

return 4->5->1->2->3->NULL.

思路:题目非常清晰。思路是先得到链表长度。再从头開始直到特定点,開始变换连接就可以。

代码例如以下:

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(k == 0 || head == null)
return head;
int len = 0;
ListNode first = head;//头结点
ListNode last = null;//尾节点
while(head != null){
len++;//求长度
last = head;//最后一个节点
head = head.next;//循环条件
} k = k%len;//假设k>len,取余数
int n = 0;
head = first;//标记到头结点
while(head!= null){
if(++n == (len - k))//推断是否到达位置
break;
head = head.next;
}
//下面为交换位置
last.next = first;
first = head.next;
head.next = null;
return first;
}
}/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode rotateRight(ListNode head, int k) {
if(k == 0 || head == null)
return head;
int len = 0;
ListNode first = head;//头结点
ListNode last = null;//尾节点
while(head != null){
len++;//求长度
last = head;//最后一个节点
head = head.next;//循环条件
} k = k%len;//假设k>len,取余数
int n = 0;
head = first;//标记到头结点
while(head!= null){
if(++n == (len - k))//推断是否到达位置
break;
head = head.next;
}
//下面为交换位置
last.next = first;
first = head.next;
head.next = null;
return first;
}
}

leetCode 61.Rotate List (旋转链表) 解题思路和方法的更多相关文章

  1. [LeetCode] 61. Rotate List 旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  2. [leetcode]61. Rotate List旋转链表

    Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...

  3. leetCode 86.Partition List(分区链表) 解题思路和方法

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  4. [leetcode]61. Rotate List反转链表k个节点

    类似于找链表的后k个节点 不同的是要把前边的接到后边 public ListNode rotateRight(ListNode head, int k) { //特殊情况 if (head==null ...

  5. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  6. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  7. leetCode 57.Insert Interval (插入区间) 解题思路和方法

    Insert Interval  Given a set of non-overlapping intervals, insert a new interval into the intervals ...

  8. leetCode 67.Add Binary (二进制加法) 解题思路和方法

    Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...

  9. leetCode 54.Spiral Matrix(螺旋矩阵) 解题思路和方法

    Spiral Matrix Given a matrix of m x n elements (m rows, n columns), return all elements of the matri ...

随机推荐

  1. java--Eclipse for mac 代码提示(代码助手,代码联想)快捷键修改

    Eclipse for mac 代码提示(代码助手,代码联想)快捷键修改 一.每次输入都自动提示 点击Eclipse,使其成为第一响应者,preferences->Java->Editor ...

  2. codeforces 757F - 最短路DAG+灭绝树

    Description 给定一个n个点,m条边的带权无向图,和起点S.请你选择一个点u(u!=S),使得在图中删掉点u 后,有尽可能多的点到S的最短距离改变. Solution 先建出最短路DAG,在 ...

  3. pat 团体天梯赛 L2-012. 关于堆的判断

    L2-012. 关于堆的判断 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的小顶堆H[] ...

  4. LOJ#2132. 「NOI2015」荷马史诗

    $n \leq 100000$个数字,放进$k$叉树里,一个点只能放一个数,使所有数字乘以各自深度这个值之和最小的同时,最大深度的数字最小. 哈夫曼.这是我刚学OI那段时间看到的,感觉就是个很无聊的贪 ...

  5. mybatis hashmap 输入键值对为空时,key 丢失

    参考文档:https://blog.csdn.net/lulidaitian/article/details/70941769 springMVC+mybatis查询数据,返回resultType=” ...

  6. 如何证明一个数的数根(digital root)就是它对9的余数?

    数根就是不断地求这个数的各位数之和,直到求到个位数为止.所以数根一定和该数模9同余,但是数根又是大于零小于10的,所以数根模9的余数就是它本身,也就是说该数模9之后余数就是数根. 证明: 假设有一个n ...

  7. 分布式定时任务调度系统技术解决方案(xxl-job、Elastic-job、Saturn)

    1.业务场景 保险人管系统每月工资结算,平安有150万代理人,如何快速的进行工资结算(数据运算型) 保险短信开门红/电商双十一 1000w+短信发送(短时汇聚型) 工作中业务场景非常多,所涉及到的场景 ...

  8. codevs——1154 能量项链(区间DP)

    2006年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解    题目描述 Description 在Mars星球上,每个Mars人 ...

  9. 使用PreloadJS加载图片资源

    一. 使用createjs里的LoadQueue函数实现异步加载图片,监听加载进度 1.实例对象LoadQueue加载队列对象 var queue = new createjs.LoadQueue(f ...

  10. 聊聊、Zookeeper Linux 集群服务

    今天是平安夜,先祝大家平安夜快乐.这篇文章我们来谈谈 Zookeeper Linux 集群. 为什么要集群呢?因为一台服务不够.集群是为了系统扩容,系统稳定.一台服务挂了,没关系,我还有其他的服务.集 ...