Leetcode之Rotate List 问题
来源:互联网 发布:mac os 重装系统 编辑:程序博客网 时间:2024/05/22 06:25
问题描述:
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
.
题目来源:Rotate List (详细地址:https://leetcode.com/problems/rotate-list/description/)
思路分析:
题目意思很简单吧,就是将后面k个结点移动到前面来。在这也可以和Remove Nth Node from End of List 一样,采用两个指针,一遍遍历。在这我们采用另外一种解题办法,我们可以先求出链表的长度大小,然后第二遍遍历找到倒数第k+1个节点,将后面的结点移动到前面来。这种解法的不便之处就在于它需要遍历两次,但是这种方法就不要需要额外考虑那么多临界条件。注意:在这的k是能大于n的,所以我们需要求余操作,而且fast不能跑出去了,必须停留在最后一个节点上,最后用于连接使用。另外,我们也可以采用添加dummy结点的办法,我在这就不添加该节点了。
代码:
节点定义:
遍历过程:
阅读全文
0 0
- Leetcode之Rotate List 问题
- leetcode之Rotate List
- LeetCode之Rotate List
- leetcode之Rotate List
- LeetCode之Rotate List
- leetcode之Rotate List
- 【Leetcode】之Rotate List
- leetcode:单链表之Rotate List
- Leetcode之Rotate Array 问题
- Leetcode之Rotate Image 问题
- [leetcode][list] Rotate List
- leetcode 之Rotate List 解题思路
- LeetCode 之 Rotate List — C 实现
- LeetCode进阶之路(Rotate List)
- LeetCode: Rotate List
- LeetCode Rotate List
- LeetCode : Rotate List
- [Leetcode] Rotate List
- Qt 学习之路 2(69):进程
- 软件测试工程师必读图书
- JSP代码练习(六)
- 《数据结构》实验二:线性表综合实验——(5)间接寻址
- Servlet与MVC分层开发
- Leetcode之Rotate List 问题
- 【线段树+Hash】Codeforces 452F Permutation
- 一点一点学线程(四)concurrent包
- xshell连接树莓派lxpanel配置
- Codeforces Round #438 题解
- 数据结构——顺序表有序顺序表归并
- 搜集糖果
- 链表的相应操作
- OS之并发与并行,异步和多线程的区别