• LeetCode-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:
        ListNode *rotateRight(ListNode *head, int k) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(k==0)return head;
            if(head==NULL)return NULL;
            ListNode* tail,*ret,*ptr;
            tail=head;
            while(tail->next!=NULL)tail=tail->next;
            if(tail==head)return head;
            for(int i=0;i<k;i++){
                ptr=head;
                while(ptr->next!=tail)ptr=ptr->next;
                tail->next=head;
                head=tail;
                ptr->next=NULL;
                tail=ptr;
            }
            return head;
        }
    };
    View Code
  • 相关阅读:
    Uboot USB模式(RK3288变砖头的解决办法)
    C++ 解析一
    C++基础
    shell脚本分析二
    ASCII
    POJ 1970 The Game (DFS)
    PoJ1979 Red and Black (DFS)
    UVA 572 Oil Deposits (DFS)
    打印日历
    求第N个素数
  • 原文地址:https://www.cnblogs.com/superzrx/p/3337847.html
Copyright © 2020-2023  润新知