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. PowerDesigner数据模型(CDM—PDM—SQL脚本的转换流程)

    原文地址:PowerDesigner数据模型(CDM-PDM-SQL脚本的转换流程)作者:zzenglish 有图片的参考http://blog.sina.com.cn/s/blog_64742675 ...

  2. 【BZOJ4477】字符串树(可持久化Trie)

    此题花费我整整三天的功夫.还在NoiP贴吧发过贴. 最后发现trie树建新节点时信息未完全复制,真是愚蠢之极. 言归正传. 如果我们已经知道了每个点上的trie树那么询问就是sum[x]+sum[y] ...

  3. css3 实现多行文本折行

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. [LeetCode] String to Integer (atoi) 字符串

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  5. asp.net core 开发环境自定义域名及端口

    web项目上右键-> 选择属性-> 选择调试->编辑“应用url” ,再用vs启动web项目即可实现自定义url. 值得注意的是如果报“visual studio无法连接到iis e ...

  6. 详解Swift和OC的混编

    前言: 我们在一些情况下,仅仅使用swift 是无法完成一个项目的,在swift项目中必要用到 OC 实现一些功能,比如,项目要使用一些第三方的框架,但这个第三方的框架却是用 OC 实现的,或者你的项 ...

  7. Network | Cookie and Session

    Cookies are arbitrary pieces of data chosen by the web server and sent to the browser. The browser r ...

  8. 分享Kali Linux 2017.1镜像

     分享Kali Linux 2017.1镜像 Kali Linux官方于4月24日发布Kali Linux 2017.1版本.该版本仍然采用滚动更新方式,所以软件源为kali-rolling.至现在分 ...

  9. BZOJ 4543 2016北京集训测试赛(二)Problem B: thr

    Solution 这题的解法很妙啊... 考虑这三个点可能的形态: 令它们的重心为距离到这三个点都相同的节点, 则其中两个点分别在重心的两棵子树中, 且到重心的距离相等; 第三个点可能在重心的一棵不同 ...

  10. Create Data Block Based On From Clause Query In Oracle Forms

    Example is given below to create a data block based on From Clause query in Oracle Forms. The follow ...