【LeetCode】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.

start with an example.

Given [0,1,2], rotate 1 steps to the right
-> [2,0,1].

Given [0,1,2], rotate 2 steps to the right
-> [1,2,0].

Given [0,1,2], rotate 3 steps to the right
-> [0,1,2].

Given [0,1,2], rotate 4 steps to the right
-> [2,0,1].

So, no matter how big K, the number of steps is, the result is always the
same as rotating K % n steps to the right.


public class Solution {
public ListNode rotateRight(ListNode head, int n) {
if(head==null||head.next==null||n==0)
return head;
int len = 0;
ListNode root = head;
while(root!=null){
root=root.next;
len++;
}

ListNode fast = head;
ListNode slow = head;
int i=n%len;
if(i==0)
return head;
while(i>0&&fast!=null){
fast=fast.next;
i--;
}

while(fast.next!=null){
slow=slow.next;
fast=fast.next;
}

ListNode tem = slow.next;
fast.next=head;
slow.next=null;
return tem;

}
}

【LeetCode】Rotate List

时间: 2024-10-05 09:19:50

【LeetCode】Rotate List的相关文章

【Leetcode】Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up: Could you do this in-place? 思路:第一种思路是一层一层的进行旋转,比较直观:第二种思路则比较取巧,首先沿着副对角线翻转一次,然后沿着水平中线翻转一次. 代码一: class Solution { public: void rotate(vector<

【LeetCode】 Rotate List 循环链表

题目:rotate list 解法1: <span style="font-size:18px;">/**LeetCode Rotate List:Given a list, rotate the list to the right by k places, where k is non-negative. * 题目:循环移动链表,等价于将链表从右边数的k个节点移动到表的前方 * 思路:移动倒是简单,重点是要找到链表倒数的k个数,就等价于找到倒数第K+1个数,设置两个指针,

【leetcode】Rotate Image(middle)

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 思路:我的思路,先沿对角线对称,再左右对称. void rotate(int **matrix, int n) { //先沿对角线对称 for(int i = 0; i < n; i++) { for(int j = 0;

【leetcode】Rotate List(middle)

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. 题目关键点:k可能比链表长度还大, 需要获取链表长度len, 用 k % len 获取具体需要移动的数字. class Solution

【LeetCode】Rotate Array

Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to

【leetcode】Reverse Words in a String

问题:给定一个字符串,字符串中包含若干单词,每个单词间由空格分隔,将单词逆置,即第一个单词成为最后一个单词,一次类推. 说明:字符串本身可能包含前导空格或后导空格,单词间可能包含多个空格,要求结果中去掉前导和后导空格,单词间空格只保留一个. 与rotate函数类似,先逆置每个单词,再将所有字符串逆置. void reverseWords(string &s) { if(s.size() == 0) return; char blank = ' '; size_t len = s.size();

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->