【LeetCode】 61. Rotate List C语言

来源:互联网 发布:godaddy虚拟主机 java 编辑:程序博客网 时间:2024/06/07 02:04

LeetCode解题心得,欢迎交流! 第二日 


/** * Definition for singly-linked list. * struct ListNode { *     int val; *     struct ListNode *next; * }; */struct ListNode* rotateRight(struct ListNode* head, int k) {    if(head == NULL) return head;        struct ListNode *new_head=head;    struct ListNode *tail=head;        int i,length=1;     while(tail->next !=NULL)    {        length++;        tail=tail->next;    }        tail->next=head;  // circle the link        if(k=k%length)    {        for(i=0;i<length-k;i++)        {            tail=tail->next;        }    }        new_head=tail->next;    tail->next=NULL;    return new_head;}


0 0
原创粉丝点击