Total Accepted: 55428 Total Submissions: 250727 Difficulty: Medium

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:
int getListLength(ListNode* head)
{
int len = ;
while(head){
len++;
head = head->next;
}
return len;
}
ListNode* rotateRight(ListNode* head, int k) {
int len = getListLength(head);
k = len == ? : len - k%len ;
if(head ==NULL || k== || k==len){
return head;
}
ListNode *newHead = NULL;
ListNode *cur = head;
int cnt = ;
while(cur){
++cnt;
if(cnt == k){
newHead = cur->next;
cur->next=NULL;
break;
}
cur = cur->next;
}
ListNode* p=newHead;
while(p && p->next){
p = p->next;
}
if(p){
p->next=head;
}
return newHead;
}
};

[Linked List]Rotate List的更多相关文章

  1. [Swift]LeetCode61. 旋转链表 | 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每天一题】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]61. Rotate List旋转链表

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

  4. LeetCode 61:旋转链表 Rotate List

    ​给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...

  5. [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 ...

  6. 力扣 — Rotate List()

    题目描述: 中文: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = ...

  7. [LC] 61. Rotate List

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

  8. 【LeetCode】61. Rotate List 解题报告(Python)

    [LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...

  9. leetcode 旋转单链表

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

随机推荐

  1. @@IDENTITY在加触发器时返回错误的ID值

    表ID是自增的,所以在添加时要查一下,之前是用@@IDENTITY来查,最近在加触发器时发现返回的会是在触发器中插入语句的数据ID值,上网找了下资料,发现是因为@@IDENTITY 将返回在当前会话中 ...

  2. js 行列操作

    function insertRow() { var tbl = document.getElementById("tbCarModel"); var rowLen = tbl.c ...

  3. tomcat https jks 沃通免费证书安装 解决方案

    网上百度了一天什么没百度到,最后谷歌到了一篇文章启发之下解决之. 代理谷歌网站推荐一个,可以直接上谷歌: https://www.yundou.info ----------------------- ...

  4. 详解AJAX核心 —— XMLHttpRequest 对象 (上)

    我要说的内容都是非常基础的内容,高手就免看了,如果看了欢迎给点意见啊.新手或者对低层还不是很了解的人可以看看,帮助理解与记忆. XMLHttpRequest 对象是AJAX功能的核心,要开发AJAX程 ...

  5. oracle存储参数(storage子句)含义及设置技巧

    可用于:表空间.回滚段.表.索引.分区.快照.快照日志 参数名称 缺省值 最小值 最大值 说明 INITIAL 5(数据块) 2(数据块) 操作系统限定 分配给Segment的第一个Extent的大小 ...

  6. pyinstaller打出的EXE包执行时报错“failed to excute ”信息

    我的程序是selenium自动化脚本,打包时执行的是 Python pyinstaller -F --onefile -w  XXX.py 这样打出的包执行后提示“failed to excute s ...

  7. 图片异步加载 ,KVO

    图片异步下载类目: .h #import <UIKit/UIKit.h> typedef void (^ImageBlock)(UIImage *img); @interface UIIm ...

  8. USB HID复合设备实例—键盘+鼠标

    实现这种USB HID复合设备有两种方法,在<USB HID协议入门>一节已经讲到其中一种方法,说一个USB HID设备可以包含多种功能的报告描述符合集,这样可以实现复合设备,如带鼠标功能 ...

  9. SQL连接方式(内连接,外连接,交叉连接)

    1.内连接.左连接.右连接.全连接介绍 內连接仅选出两张表中互相匹配的记录.因此,这会导致有时我们需要的记录没有包含进来.内部连接是两个表中都必须有连接字段的对应值的记录,数据才能检索出来.   左连 ...

  10. openjpa框架入门_项目 database 启动project 初始化(三)

    mysql数据库安装好,这里不多说,现在来执行sql脚本 http://download.csdn.net/detail/shenhonglei1234/6019677 将下载好的脚本后缀名“open ...