samedi 22 mai 2021

I am trying to do the Rotate List question (https://ift.tt/3wmnCCo) my list is not updating inside the while loop

class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        //cout<<head->val;
        int temp = 0;
        ListNode *start = head;
        
        while(k--){
        
        while(start->next->next!=NULL){
            temp = start->next->val;
            start->next->val= temp;
            start=start->next;
        }
        temp = start->next->val;
        head->val = temp;
        }
   return head; }
};

The list is not updating inside the loop. Input [1,2,3,4,5] 1 Output [5,2,3,4,5] Expected [5,1,2,3,4]

Aucun commentaire:

Enregistrer un commentaire