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

双指针,找到要rotate的指针位置即可。

/**
 * 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 == nullptr)
            return nullptr;
        ListNode* pre = new ListNode(0);
        ListNode* p1 = head;
        ListNode* p2 = head;
        int len = length(head);
        k = k%len;
        if(k%len == 0)
            return head;
            
        while(k!=0){
            p2 = p2->next;
            --k;
        }
        
        ListNode* first ; //the first rotate list
        while(p2->next!=nullptr){
                p1 = p1->next;
                p2 = p2->next;
        }
        
        first = p1->next;
        p1->next = nullptr;
        pre->next = first;
        p2->next = head;
        return first;
    }
    int length(ListNode *head){
        int k = 0;
        while(head!=nullptr){
            head = head->next;
            ++k;
        }
        return k;
    }
};


你可能感兴趣的:([LeetCode]Rotate List)