[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
.
注意一些边界的处理,找到n-k+1个节点后处理移位。
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 int calListLen(ListNode *node) 12 { 13 int len = 0; 14 while(node) 15 { 16 len++; 17 node = node->next; 18 } 19 return len; 20 } 21 22 ListNode *rotateRight(ListNode *head, int k) { 23 // Start typing your C/C++ solution below 24 // DO NOT write int main() function 25 if (head == NULL) 26 return NULL; 27 28 int len = calListLen(head); 29 30 k = k % len; 31 32 if (k == 0) 33 return head; 34 35 ListNode *p = head; 36 ListNode *pPre = NULL; 37 38 for(int i = 0; i < len - k; i++) 39 { 40 pPre = p; 41 p = p->next; 42 } 43 44 ListNode *q = p; 45 while(q->next) 46 q = q->next; 47 48 if (pPre) 49 pPre->next = NULL; 50 51 q->next = head; 52 53 return p; 54 } 55 };
[LeetCode] Rotate List相关推荐
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- LeetCode Rotate Array(数组的旋转)
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- LeetCode Rotate Image(矩阵的旋转)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- LeetCode:Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- [LeetCode]Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- leetcode Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- leetcode Rotate Image
Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...
- [LintCode/LeetCode] Rotate List
Problem Given a list, rotate the list to the right by k places, where k is non-negative. Example Giv ...
- LeetCode Rotate Function(寻找规律)
题意:给出一个数组a,长度为n 其中f(0)=0*a[0]+1*a[1]+...+(n-1)*a[n-1] f(1)=0*a[n-1]+1*a[0]+...+(n-1)*a[n-2] ... f(n- ...
最新文章
- 笛卡尔积 php,PHP自定义函数生成笛卡尔积
- .NET三种事务处理详解
- js中当等于最小值是让代码不执行_JavaScript中最最基础的知识点
- 细说shiro之三:在独立应用中使用shiro
- C语言实现与功能的程序,用C语言实现Ping程序功能
- linux上安装fio教程,fio工具安装及使用
- 【今日CS 视觉论文速览】Part2, 16 Jan 2019
- Matplotlib 中文用户指南 3.2 图像教程
- python nltk book_nltk book的下载
- linux shell 命令执行结果,如何通过程序执行shell命令并获取命令执行结果?
- IEtester不靠谱
- 阶段3 2.Spring_09.JdbcTemplate的基本使用_6 JdbcDaoSupport的使用以及Dao的两种编写方式...
- pandaboard 安装_pandaboard ES学习之旅——3 Uboot源码下载与编译
- window7DOS常用命令
- Windows11右键桌面没新建
- 俄亥俄州立大学哥伦布分校计算机科学,俄亥俄州立大学哥伦布分校什么专业最好?...
- java 加水印_Java添加水印(图片水印,文字水印)
- 广告策略评估指标(算法实习day2)
- 苹果备份有什么用_数据备份用什么软件好?好用的数据备份软件分享
- 我的政治理想《爱因斯坦文集》
热门文章
- mybatis-plus中的问题总结
- win8.1升级到win10后 vmware不能连网的问题
- 【读书笔记】iOS-属性
- Bootstrap排版中地址与引用详解
- (转)json-lib 的maven dependency
- SQL Server 2012 sa 用户登录 18456 错误
- SQL--数据库性能优化详解
- 药品查询APP开发流程(七)--开发—yao.js
- 如何设计出“有趣”的互联网产品?
- Creating an Extender Control to Associate a Client Behavior with a Web Server Control