zoukankan      html  css  js  c++  java
  • Rotate List || LeetCode

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* rotateRight(struct ListNode* head, int k) {
        struct ListNode *sentry,*p,*temp,*tail;
        int    len=0,index;
        
        if(head==NULL)return NULL;
        sentry=(struct ListNode *)malloc(sizeof(struct ListNode));
        sentry->next=NULL;
        p=head;
        tail=sentry;
        while(1){
            temp=(struct ListNode*)malloc(sizeof(struct ListNode));
            temp->val=p->val;
            temp->next=NULL;
            tail->next=temp;
            tail=temp;
            len++;
            if(p->next==NULL)break;
            p=p->next;
        }
        p->next=sentry->next;
        k=k%len,index=0;
        p=head;
        while(p){
            if(index==(len-k)){
                temp=p;
            }
            if(index==(2*len-k-1)){
                p->next=NULL;
                break;
            }
            index++;
            p=p->next;
        }
        return temp;
    }
  • 相关阅读:
    蒟蒻的填坑计划
    现在的状态....
    date modify
    set source
    image source
    simple auth
    net
    bridge
    iptable
    namespace
  • 原文地址:https://www.cnblogs.com/ProtectedDream/p/4558823.html
Copyright © 2011-2022 走看看