Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- knakul853
- */
- /**
- knakul853
- **/
- class Solution {
- public:
- ListNode* removeNthFromEnd(ListNode* head, int n) {
- int len = 0;
- ListNode* cur = head;
- while(cur){
- cur = cur->next;
- len++;
- }
- if( n > len) return NULL;
- if(len == n )
- {
- return head->next;
- }
- n = len - n;
- n--;
- cur = head;
- while(cur && n>0)
- {
- cur = cur->next;
- n--;
- }
- cur->next = cur->next->next;
- return head;
- }
- };
Add Comment
Please, Sign In to add comment