leetcode 旋转单链表
Given a linked list, rotate the list to the right by k places, where k is non-negative.
Example 1:
Input: 1->2->3->4->5->NULL, k = 2
Output: 4->5->1->2->3->NULL
Explanation:
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL
Example 2:
Input: 0->1->2->NULL, k = 4
Output:2->0->1->NULL
Explanation:
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right:0->1->2->NULL
rotate 4 steps to the right:2->0->1->NULL
关键:
1、3个指针,一个指向打断处的后一个结点,并且将要成为新链表头,ListNode* front,用于return;一个指向打断处的前一个节点,并成为链表尾,ListNode* back,用于将尾节点的next置空;一个为当前的尾巴节点,ListNode* link 用于将当前尾节点与头节点连起来。
2、2次O(n)级的遍历,第一次用于求出链表长度,进而求出front和end位置;第二次用于将front和end移动到求出的位置
3、4种特殊情况,一是链表长度为0;二是链表长度为1;三是旋转的步数大于链表长度时要取模;四是旋转步数取模后为0时直接返回。
/**
* 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 NULL;
}else if(head->next==NULL){
return head;
} ListNode* front=head;
ListNode* back=head;
ListNode* link=head; int len=;
while(link->next!=NULL){
len++;
link=link->next;
} k=k%len;
if(k==){
return head;
}
int step=len-k-;
front=front->next; for(int i=;i<step;i++){
back=back->next;
front=front->next;
} back->next=NULL;
link->next=head;
return front;
}
};
leetcode 旋转单链表的更多相关文章
- leetCode题解单链表反转
1.题目描述 反转一个单链表.链表节点结构如下: struct ListNode { int val; ListNode* next; }; 2.问题分析 特殊情况是输入的头结点是一个空的,或者只有一 ...
- LeetCode 206 单链表翻转
https://leetcode.com/problems/reverse-linked-list/ 思路很简单,分别设置三个结点,之后依次调整结点1和结点2的指向关系. Before: pre -& ...
- leetcode 去除单链表倒数第k个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode 笔记系列六 Reverse Nodes in k-Group [学习如何逆转一个单链表]
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- [LeetCode 206] Reverse Linked List 翻转单链表
本题要求将给定的单链表翻转,是校招面试手撕代码环节的高频题,能很好地考察对单链表这一最简单数据结构的理解:可以使用迭代和递归两种方法对一个给定的单链表进行翻转,具体实现如下: class Soluti ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
随机推荐
- Caliburn Micro框架快速上手(WP)
一.使用nuget添加起始工程 二.修改App.xaml文件和App.xaml.cs文件 AppBootstrapper介绍: AppBootstrapper根据中文的直译可以 ...
- XmlFactoryBean和DefaultListableBeanFactory学习
首先提供了一个Spring容器最简单的例子. bean的定义,MyTestBean.java public class MyTestBean { private String testStr = &q ...
- 示例 - C#脚本代码采集搜狐NBA球员, 球队和比赛实况
最近 @甜瓜 (QQ:1069629945) 开发了一套NBA数据采集脚本, 我觉得很赞. 经他允许发布出来和大家分享一些经验: 球员球队: http://data.sports.sohu.com/n ...
- sqlite3命令读出sqlite3格式的文件内容案例
/********************************************************************* * Author : Samson * Date ...
- 第二百六十二节,Tornado框架-cookie
Tornado框架-cookie Cookie 是网站用来在客户端保存识别用户的一种小文件.一般来用库可以保存用户登 录信息.购物数据信息等一系列微小信息. self.set_cookie()方法,创 ...
- 【BZOJ】1642: [Usaco2007 Nov]Milking Time 挤奶时间(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1642 果然没能仔细思考是不行的.. 以后要静下心来好好想,不要认为不可做..... 看了题解... ...
- jQuery实现瀑布流布局详解(PC和移动端)
首先我们将如下样式的若干个单元写进body中,并将“box”向左浮动: <div class="box"> <img class="img" ...
- iOS 模块化
模块化 1.公共模块 网络层 模型层(基类) 2.mvvm 3.模块化(单元模块,实现单元功能,单元测试) 4.pod 5.路由
- 大圣画廊v0.2(2015.7.17)
0.2版本号加入的功能 以tag分类图片 美化.添加瀑布流效果 添加tag页和单张图片页 添加公布图片页 以下是具体解释. 每添加一个功能,都要从模型.模板,视图,路由四个方面一一改动. 模型:添加t ...
- WPF进阶之接口(2):IDisposable,ICollectionView
废话不多说,进入正题,先来说说IDisposable,看例子(来自MSDN): using System; using System.ComponentModel; // 下面的例子将展示一个实施了I ...