Question:
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: ListNode *rotateRight(ListNode *head, int k) { if(!head) return NULL; if(k<=0) return head; if(!head->next) return head; ListNode *dh = new ListNode(0); dh->next = head; ListNode *pre = dh, *fur = head; for(;k>1;k--) { if(!fur->next) fur = dh->next; else fur=fur->next; } if(!fur->next) { delete dh; return head; } for(;fur->next;) { pre=pre->next; fur=fur->next; } fur->next = dh->next; dh->next = pre->next; pre->next = NULL; ListNode *ret = dh->next; delete dh; return ret; } };
欢迎关注微信公众号——计算机视觉
相关推荐
第1章 C++编程技术 2 1.1 类和对象 2 1.2 类的继承 5 1.3 函数重载 5 1.4 访问控制 7 1.5 操作符重载 8 1.6 显式类型转换 9 1.7 异常处理 13 1.8 名字空间 17 1.9 友员函数 20 1.10 内联函数 ...
第1章 C++编程技术 2 1.1 类和对象 2 1.2 类的继承 5 1.3 函数重载 5 1.4 访问控制 7 1.5 操作符重载 8 1.6 显式类型转换 9 1.7 异常处理 13 1.8 名字空间 17 1.9 友员函数 20 1.10 内联函数 ...
第1章 C++编程技术 2 1.1 类和对象 2 1.2 类的继承 5 1.3 函数重载 5 1.4 访问控制 7 1.5 操作符重载 8 1.6 显式类型转换 9 1.7 异常处理 13 1.8 名字空间 17 1.9 友员函数 20 1.10 内联函数 ...
* [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...
34.40 rotate和rotate_copy 34.41 search 34.42 search_n 34.43 set_difference 34.44 set_intersection 34.45 set_symmetric_difference 34.46 set_union 34.47 sort 34.48 sort_heap 34.49 stable_partition 34.50...
RotateList LeetCode 75 Sort Colors LeetCode 125 Valid Palindrome LeetCode 167 Two Sum II - Input array is sorted LeetCode 344 Reverse String LeetCode 345 Reverse Vowels of a String 2 字符串 编号 题目 ...
Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group Copy List with Random Pointer Linked List Cycle Linked List Cycle II Reorder List LRU Cache Palindrome Linked ...
rotate.c -- Three ways to rotate the elements of a vector. The next two program are used in a pipeline to compute all anagrams in a dictionary sign.c -- Sign each word by its letters in sorted order...
内容简介回到顶部↑这本书不适合C++ 初学者,不适合 Genericity(泛型技术)初学者,或 STL 初学者。这本书也不适合带领你学习面向对象(Object Oriented)技术 — 是的,STL 与面向对象没有太多关连。本书前言清楚...
c++ 标准规格定案前,hp规范的stl头文件(扩展名 .h) 017 sgi stl 内部文件(sgi stl真正实现于此) 018 1.8.3 sgi stl 的组态设定(configuration) 019 1.9可能令你困惑的c++ 语法 026 1.9.1 stl_config.h 中...
WOW模型查看器,有源代码. WoWmodelview - World of Warcraft 3D model viewer This program displays 3D models from World of Warcraft: characters, creatures, spell ...- Equipment: choose from the popup list
(FActionIndex could appear beyond the list boundaries). Thanks to Giedrius Matonis. - ADD: Added the method TCustomProp.Assign - a source flex-property is written to stream, which is then read by...
Written with Microsoft Visual C++ 6.0. Uses Microsoft SNMP API. Features: Single graph displays changes of two configurable SNMP variables with display of Current, Average, Maximum values. Changes ...
cube.zip This example demonstrates how to rotate a cube in visual basic.<END><br>17 , sprite1.zip This is an Excellent example on how to use sprites in your program.<END><br>18 , charcreate.zip...
docking.zip This ActiveX control and Demo will allow you to dock your toolbars/forms to a mdiform much in the way in which Visual C++ allows you to. <END><br>17 , resizable7segment_source.zip An...