[leetcode-61-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.
思路:
首先求出list的长度length,然后将最后一个结点指向初始的头结点,
然后从该初始结点出发,指针向后移动length-k次,
找到新链表的尾结点,然后把它的next置为NULL。
ListNode* rotateRight(ListNode* head, int k){ListNode* result;if (k == 0 || head == NULL) return head;int length = 1;ListNode* temp = head;while (head != NULL && head->next != NULL){head = head->next;length++;}k %= length;head->next = temp;//head 目前为最后一个结点的指针 指向初始时候的头部结点for (int i = 0; i < length - k-1;i++){temp = temp->next;}result = temp->next;temp->next = NULL;return result;}
转载于:https://www.cnblogs.com/hellowooorld/p/6674159.html
[leetcode-61-Rotate List]相关推荐
- leetcode 61 Rotate List ----- java
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- LeetCode 61. Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- leetcode 61. Rotate List
Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 定义两个指 ...
- 【Leetcode -61.旋转链表 -82.删除排序链表中的重复元素Ⅱ】
Leetcode Leetcode -61.旋转链表 Leetcode -82.删除排序链表中的重复元素Ⅱ Leetcode -61.旋转链表 题目:给你一个链表的头节点 head ,旋转链表,将链表 ...
- [勇者闯LeetCode] 189. Rotate Array
[勇者闯LeetCode] 189. Rotate Array Description Rotate an array of n elements to the right by k steps. F ...
- Leetcode Golang 61. Rotate List.go
思路 先把list变成一个环,然后转到指定位置 注意有个取模的处理 code func rotateRight(head *ListNode, k int) *ListNode {if head == ...
- 61. Rotate List
题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example: Gi ...
- LeetCode 61~65
前言 本文隶属于专栏<LeetCode 刷题汇总>,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢! 本专栏目录结构请见LeetCode 刷题汇总 正文 幕布 幕 ...
- [LeetCode 题解]: Rotate List
Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...
- LeetCode 61. 旋转链表
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = 2 输出: 4-& ...
最新文章
- 【AI参赛经验】汉字书法识别比赛经验心得——by:microfat_htu
- python获取系统时间月份_python 取数组绝对值python获取当前日期
- 第一次冲刺对各组的评价
- 服务机器人平台和后台
- 【CI/CD2】actions,daocloud
- 5G NR Operating bands and channel bandwidth
- JavaSE | 多线程
- linux之gdb基本调试命令和使用总结
- php thread linux,Linux_linux内核函数kernel_thread,设备驱动程序中,如果需要几 - phpStudy...
- 编译单元必须以 java_java中什么是编译单元
- python table_Python PrettyTable示例
- Spelling, Edit Distance, and Noisy Channels 拼写、编辑距离和噪声通道
- 基于Go语言Beego+Layui的OA办公系统
- 越豪华越危险 家装豪华程度与环境污染成正比
- ASPICE_SWE.1_01_01_QA
- 观视界Grandvision EDI项目案例
- 第十天文件包含漏洞 php伪协议
- CentOS 之 openssl-devel 安装
- 文献阅读07期:智网中动态电价对太阳能板普及的影响
- HTTP协议响应状态码(301、302、303的区别)
热门文章
- Windows Performance Toolkit
- 使用selector修改TextView中字体的颜色
- zabbix学习(四)IT_Service管理
- 使用虚拟机运行Ubuntu时,主机与宿主机共享文件的方法。
- 【C++】函数指针的嵌套
- 请确保此代码文件中定义的类与“inherits”属性匹配,并且该类扩展的基类(例如Page 或UserControl)是正确的。...
- python 的回调函数
- 阿里巴巴2014校招笔试错误汇总
- Asp.net的Session和Cookie传值方式
- VS2010 COM组件问题