Why my answer is Wrong?


  • 0
    G

    /**

    • 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==NULL||k==0||head->next==NULL)
      return head;
      //ListNode *preNode = NULL;
      ListNode *slow = head;
      ListNode *fast = head;
      for(int i=0; i<k; i++){
      fast = fast->next;
      }
      while(fast != NULL || fast->next!=NULL){
      //preNode = slow;
      slow = slow->next;
      fast = fast->next;
      }
      fast->next = head;
      head = slow->next;
      slow->next = NULL;
      return head;
      }
      };

  • 0
    M

    would u insert it in code format to be understandable ?!


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.