[C + +] leetcode:76 Rotate List

Source: Internet
Author: User

Topic:

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 .

Give a list, and a non-negative k, requires counting from the end of the chain, K-step rotation, to get a new list.

given [0,1,2" , rotate 1 steps to the right-> [2,0,1] .

given [0,1,2" , rotate 2 steps to the right-> [1,2,0] .

given [0,1,2" , rotate 3 steps to the right-> [0,1,2] .

Given [0,1,2] , rotate 4 steps to the right [2,0,1] .

Method: First ring, then break the chain list.

Idea: Because the number of rotation nodes K may be 0, it may also be more than the length of the list, so we need to take the K surplus.

The cursor pointer moves backwards from the head node, when it points to the tail node, gets the linked list length Len, and then connects the list to the tail, then the cursor moves back to K Len to get the tail node of the resulting list. Finally, the head points to the next node of the result tail node, then disconnects the list (trailing node->next null)

Attention:

1. Since the rotation, we did not change the relative node order (such as 1-2-3-4,k=2, become 3-4-1-2), so you can first form a chain ring, and then ask for the position to be disconnected. This method is very ingenious and should be studied. (At this point, p points to the last node of the original list)

(2) Make the chain list tail-        p->next = head;

2. You need to consider the different values of k, k = 0, K<=len, k> Len, take the remainder to get relative rotation position.

(3) Find the end node of the result list        k%= len;        int step = len-k;
3. Note Initially, if the list has no nodes, return to head, excluding special cases.

if (head = = NULL) return head;
Complexity: O (N)


AC Code:

/** * Definition for singly-linked list. * struct ListNode {*     int val; *     ListNode *next; *     listnode (int x): Val (x), Next (NULL) {} *}; */class Soluti On {public:    listnode *rotateright (listnode *head, int k) {        if (head = = NULL) return head;        listnode* p = head;        int len = 1;        (1) Calculate the list length while        (p->next! = NULL)        {            p = p->next;            len++;        }                (2) Make the chain list tail-        p->next = head;                (3) Find the end node of the result list        k%= len;        int step = len-k;        while (step > 0)        {            p = p->next;            step--;        }                (4) Break the list, from the end of the list of results linked to the location of the new head node, is the result of the end of the chain footer node next nodes. Result chain footer node->next null        head = p->next;        P->next = NULL;                return head;    };


[C + +] leetcode:76 Rotate List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.