remove nth node from end of list one iteration code example
Example: remove nth node from end of list
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode slow = head, fast = head;
int count = 0;
while(fast != null && fast.next != null){
fast = fast.next;
if(count >= n)
slow = slow.next;
count++;
}
if(count+1 == n) return slow.next;
slow.next = slow.next.next;
return head;
}
}