knakul853

Untitled

Jun 16th, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. /**
  2. knakul853
  3.  */
  4. class Solution {
  5. public:
  6.     ListNode* removeNthFromEnd(ListNode* head, int n) {
  7.        
  8.        
  9.         int len = 0;
  10.         ListNode* cur = head;
  11.        
  12.         while(cur){
  13.             cur = cur->next;
  14.             len++;
  15.         }
  16.        
  17.         if( n > len) return NULL;
  18.        
  19.         if(len == n )
  20.         {
  21.             return head->next;
  22.         }
  23.         n = len - n;
  24.         n--;
  25.         cur = head;
  26.         while(cur && n>0)
  27.         {
  28.             cur = cur->next;
  29.             n--;
  30.         }
  31.            cur->next = cur->next->next;
  32.         return head;
  33.        
  34.        
  35.        
  36.     }
  37. };
Add Comment
Please, Sign In to add comment