题目

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.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if (!head || !(head->next)) return head;
ListNode *p = head;
int len = ;
for (;p->next;++len,p=p->next){}
ListNode *end = p;
k = k % len;
p = head;
for (size_t i = ; i < len-k-; ++i)
{
p = p->next;
}
end->next = head;
head = p->next;
p->next = NULL;
return head;
}
};

Tips:

思路很简单,需要注意的是对指针的操作。

=====================================

第二次过这道题:

(1)没有考虑k比ListNodes长度大的情况

(2)没考虑k加上双指针的边界情况没有考虑完全,所以几次都没有AC。

完备的思路应该是:先求出来ListNodes的长度,k%len就是真正要rotate的元素。搞清楚这之后,再利用快慢指针常规思路就可以解出来了。

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if ( !head || !head->next ) return head;
int len = ;
for ( ListNode* p = head; p->next; ++len, p=p->next){}
k = k % len;
ListNode dummpy(-);
dummpy.next = head;
ListNode* p1 = head;
ListNode* p2 = head;
for ( int i=; i<k; ++i ) p2 = p2->next;
while ( p2->next )
{
p1 = p1->next;
p2 = p2->next;
}
p2->next = head;
dummpy.next = p1->next;
p1->next = NULL;
return dummpy.next;
}
};

【Rotate List】cpp的更多相关文章

  1. 【Rotate Image】cpp

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  2. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  3. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  4. leetcode 【Rotate List 】python 实现

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  5. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. leetcode 【 Rotate Image 】python 实现

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  7. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  8. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  9. 【Sort Colors】cpp

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

随机推荐

  1. MATLAB之数据处理+公式拟合

    MATLAB之数据处理+公式拟合 前言:由试验得到一组数据,对该组数据进行处理,作图分析,分析各变量的关系,期望得到拟合公式. 试验数据背景 本次试验有三个自变量:V.M.G,因变量为F,每组试验重复 ...

  2. zblog添加水印插件后出现Cannot use $this as parameter

    安装了水印插件后后台也进不去了,页面错误提示:Cannot use $this as parameter 删除水印插件文件后恢复正常,具体原因待研究 水印插件文件:/zb_users/plugin/W ...

  3. python3基础06(随机数的使用)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import osimport randomimport string la=[0,1,2,3,4,5,6,7, ...

  4. jQuery_2_常规选择器-进阶选择器

    进阶选择器: 1. 群组选择器     $("span,em,#box")   获取多个选择器的DOM对象 <div id="d1">div< ...

  5. jq实现剪裁图片设置为头像

    有时候我们需要设置为这样,就是将某些图片设置为剪裁成设置的尺寸:就是这样的 插件的地址: http://www.htmleaf.com/jQuery/Image-Effects/20150421171 ...

  6. 浅析Dagger2依赖注入实现过程

    Dragger2是Android应用开发中一个非常优秀的依赖注入框架.本文主要通过结合Google给出的MVP开发案例todo-mvp-dagger(GitHub连接地址:https://github ...

  7. 配置Maven镜像与本地缓存

    IntelliJ IDEA 安装后自带Maven,也可以使用自己安装的Maven. 配置阿里镜像与本地仓库文件夹 找到Maven的安装目录 打开settings.xml配置文件   修改mirrors ...

  8. URL跨项目调用方法,获取返回的json值,并解析

    package com.mshc.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  9. python内置函数map/reduce/filter

    python有几个内置的函数很有意 思:map/filter/reduce,都是对一个集合进行处理,filter很容易理解用于过滤,map用于映射,reduce用于归并. 是python列表方法的三架 ...

  10. jQuery入门第一天-(一个菜鸟的不正经日常)

    jQuery的初步认识 菜鸟Q1:什么是jQuery? jQuery就是一个JavaScript函数库,没什么 特别的. 菜鸟Q2:jQuery能做什么?jQuery是做什么的? jQuery本身就是 ...