My cpp solution, 3ms,beats 100% cpp submission(although i dk why)


  • -1
    W

    '''
    /**

    • Definition for singly-linked list.
    • struct ListNode {
    • int val;
      
    • ListNode *next;
      
    • ListNode(int x) : val(x), next(NULL) {}
      
    • };
      /
      class Solution {
      public:
      ListNode
      removeNthFromEnd(ListNode* head, int n) {
      ListNode *current = head,*n_before = head;
      int cnt(0);
      while(current->next!=NULL){
      if(cnt-n>=0) n_before = n_before->next;
      current = current->next;
      cnt++;
      }
      if(cnt-n==-1) return head->next;
      if(cnt-n<-1) return head;
      n_before->next = (n_before->next->next);
      return head;
      }
      };
      '''

Log in to reply
 

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