knakul853

Untitled

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