leetcode:Rotate List

2015-11-21 01:05:09 · 作者: · 浏览: 8
/**
 * 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) {
        ListNode *p=head;
        int num = 1;
        if(!p)
            return NULL;
        while(p->
next) { p = p->next; num++; } p->next = head; int step = num-k%num; for(int i=0;i next; } head = p->next; p->next = NULL; return head; } };