`

Rotate List (C++)

 
阅读更多

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;
    }
};

 

 欢迎关注微信公众号——计算机视觉 

 

0
1
分享到:
评论

相关推荐

    C++ STL 开发技术导引(第6章)

    第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 内联函数 ...

    C++ STL开发技术导引(第5章)

    第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 内联函数 ...

    C++ STL开发技术导引(第3章)

    第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 内联函数 ...

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    -C++参考大全(第四版) (2010 年度畅销榜

    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...

    javalruleetcode-LeetCode:LeetCode算法问题

    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 字符串 编号 题目 ...

    cpp-算法精粹

    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...

    STL 源码剖析(侯捷先生译著)

    内容简介回到顶部↑这本书不适合C++ 初学者,不适合 Genericity(泛型技术)初学者,或 STL 初学者。这本书也不适合带领你学习面向对象(Object Oriented)技术 — 是的,STL 与面向对象没有太多关连。本书前言清楚...

    STL源码剖析.pdg

    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 中...

    wowmodelview 含源代码

    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

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    (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...

    STG (SNMP Traffic Grapher)

    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 ...

    VB编程资源大全(英文源码 其它)

    cube.zip This example demonstrates how to rotate a cube in visual basic.&lt;END&gt;&lt;br&gt;17 , sprite1.zip This is an Excellent example on how to use sprites in your program.&lt;END&gt;&lt;br&gt;18 , charcreate.zip...

    VB编程资源大全(英文源码 控件)

    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. &lt;END&gt;&lt;br&gt;17 , resizable7segment_source.zip An...

Global site tag (gtag.js) - Google Analytics