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 andk=2,
return 4−>5−>1−>2−>3−>NULL.
分析
给定一个链表,以及一个整数k,返回链表右旋k个元素后的结果。
要使得链表右旋k个元素,也就是说明链表后(k)个元素将成为新链表的前半部分,原链表的前(len−k)个元素将成为新链表的后半部分;
此时原链表的head前恰好有k 个元素,即完成了右旋k个位置。
要注意的是,当k=0||n∗len时,原链表将不变,只有当k的结果为 1 len−1时,链表才会发生变化。
AC代码
/**
* 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 == NULL)
return head;
ListNode *p = head;
//求链表的长度
int len = 0;
while (p)
{
len++;
p = p->next;
}
k %= len;
//k<=0时,原链表不旋转
if (k <= 0)
return head;
int index = 1;
//寻找右旋k位置后,链表的首结点
p = head;
while (index < (len - k) && p->next != NULL)
{
index++;
p = p->next;
}
ListNode *ret = p->next, *q = p;
//原链表寻找尾结点,将其链接到head
while (p->next)
p = p->next;
p->next = head;
//前部分尾结点设为NULL
q->next = NULL;
return ret;
}
};
LeetCode(61) Rotate List的更多相关文章
- LeetCode(189) Rotate Array
题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the arr ...
- LeetCode(61):旋转链表
Medium! 题目描述: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, ...
- LeetCode(48)Rotate Image
题目 You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise ...
- Qt 学习之路 2(61):使用 SAX 处理 XML
Qt 学习之路 2(61):使用 SAX 处理 XML 豆子 2013年8月13日 Qt 学习之路 2 没有评论 前面两章我们介绍了使用流和 DOM 的方式处理 XML 的相关内容,本章将介绍 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- Reshapeing operations
Reshapeing operations Suppose we have the following tensor: t = torch.tensor([ [1,1,1,1], [2,2,2,2], ...
- iOS端样式错位
在iOS端上点击的时候触发点会在当前元素上方,原因是在最外层使用了fixed定位,换成绝对或相对定位解决问题
- Robot Framework问题汇总...不断更新中
在实际使用Robot Framework工具过程中,难免会遇到一些问题, 我们将会一一记录下来,以便后来者碰到类似的问题能够快速解决! 安装类问题: ========================= ...
- [转]广义正交匹配追踪(gOMP)
广义正交匹配追踪(Generalized OMP, gOMP)算法可以看作为OMP算法的一种推广,由文献[1]提出,第1作者本硕为哈工大毕业,发表此论文时在Korea University攻读博士学位 ...
- _bzoj1036 [ZJOI2008]树的统计Count【树链剖分】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 保存模版. 执行qmax与qsum操作,往上爬的时候最开始的代码出了点小问题,往上爬的 ...
- Optimizing Downloads for Efficient Network Access
Optimizing Downloads for Efficient Network Access Previous Next 1.This lesson teaches you to Unders ...
- ios 创建和绘画pdf文件 -转
转自:http://blog.csdn.net/ant1239/article/details/7761676 本方法为项目中画pdf的一个方法,画pdf,一共分为几步,1,获取地址,有两种获取地址方 ...
- 转 SQLPLUS中SQL换行执行
权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下,在SQLPLUS中输入命令时,可以换行,但不能有空格,否则不能执行,会直接返回到SQL>下.但通过命令设置可以实现语句换行时允许有 ...
- JSP页面自动刷新
1.页面自动刷新:把如下代码加入<head>区域中<meta http-equiv="refresh" content="20">,其中 ...
- Apache Tomcat 之路(二 部署web 应用程序)
1.创建一个webapplication,不论是解压的应用程序包还是war包,在tomcat 上都能部署,这里提供一个简单的web项目:git地址:https://github.com/coderxi ...