[Linked List]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
.
- /**
- * Definition for singly-linked list.
- * struct ListNode {
- * int val;
- * ListNode *next;
- * ListNode(int x) : val(x), next(NULL) {}
- * };
- */
- class Solution {
- public:
- int getListLength(ListNode* head)
- {
- int len = ;
- while(head){
- len++;
- head = head->next;
- }
- return len;
- }
- ListNode* rotateRight(ListNode* head, int k) {
- int len = getListLength(head);
- k = len == ? : len - k%len ;
- if(head ==NULL || k== || k==len){
- return head;
- }
- ListNode *newHead = NULL;
- ListNode *cur = head;
- int cnt = ;
- while(cur){
- ++cnt;
- if(cnt == k){
- newHead = cur->next;
- cur->next=NULL;
- break;
- }
- cur = cur->next;
- }
- ListNode* p=newHead;
- while(p && p->next){
- p = p->next;
- }
- if(p){
- p->next=head;
- }
- return newHead;
- }
- };
[Linked List]Rotate List的更多相关文章
- [Swift]LeetCode61. 旋转链表 | Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 【LeetCode每天一题】Rotate List(旋转链表)
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- [leetcode]61. Rotate List旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- LeetCode 61:旋转链表 Rotate List
给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. Given a linked list, rotate the list to the right by k pla ...
- [LeetCode] 61. Rotate List 旋转链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 力扣 — Rotate List()
题目描述: 中文: 给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: 1->2->3->4->5->NULL, k = ...
- [LC] 61. Rotate List
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
- 【LeetCode】61. Rotate List 解题报告(Python)
[LeetCode]61. Rotate List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fux ...
- leetcode 旋转单链表
Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: I ...
随机推荐
- Reverse Linked List II java
public static ListNode reverseBetween(ListNode head, int m, int n) { ListNode pre=head,current=head, ...
- 二维数组在text,image的应用
NSArray *imageArr = @[@[@"查看地图",@"map_hy.png"], @[@"联系号码",@"phone ...
- python 基础篇(二)数据类型概述
正式进入python的学习. 数据类型可以分为身份,类型,数据项三项联合组成. 身份: id() 类型:type() 数据类型:int,boolean,tuple,string,dict,list 1 ...
- VirtualBox添加共享文件夹
直接上图 添加了一个名为"Ubuntu10.04-en"的共享文件夹 但是按照它说的命令 mount -t vboxsf share mount_point 打入,然后悲剧了 错误 ...
- Uncaught TypeError: Object [object Object] has no method 'live'
$( selector ).live( events, data, handler ); // jQuery 1.3+$( document ).delegate( se ...
- mac/linux install hg
MAC OSX 10.9: sudo port -v install mercurial or easy_install mercurial
- python之列表、字典的使用
一.概述:以后你在Linux里面写Python脚本的时候会经常用到Python列表.字典,因为你在以后写脚本的时候,大多数情况下都是对文件进行操作处理,使用字典和列表可以很好的操作文件,得出你想要的结 ...
- Python第一天-----简单登录验证
----------------------------------------- 编写登录接口 要求:1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定 -------------- ...
- 翻译题(map使用)
What Are You Talking About 点我 Problem Description Ignatius is so lucky that he met a Martian yesterd ...
- POJ 1631 Bridging signals & 2533 Longest Ordered Subsequence
两个都是最长上升子序列,所以就放一起了 1631 因为长度为40000,所以要用O(nlogn)的算法,其实就是另用一个数组c来存储当前最长子序列每一位的最小值,然后二分查找当前值在其中的位置:如果当 ...