Delete Nth node from the end of the given linked list Solution .
For Explanation Watch Video :
Code::
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
int count = 0;//5
ListNode temp1 = head;
while(temp1!=null){
count++;
temp1 = temp1.next;
}
if(count==n){
return head.next;
}
ListNode temp2 = head;
int remove = count - n;//3
int count1 = 0;
while(count1!=remove-1){
temp2 = temp2.next;
count1++;
}
temp2.next = temp2.next.next;
return head;
}
}
Comments
Post a Comment