Remove Nth Node From End of List Solution LEET CODE
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode node=head;
int count=0;
while(node!=null)
{
node=node.next;
count++;
}
int new_count=count-n;
node=head;
while(--new_count>0)
{
node=node.next;
}
if(node==head && head.next==null)
{
head=head.next;
return head;
}
else if(count-n==0)
{
head=head.next;
return head;
}
else
{
ListNode temp=node.next;
node.next=temp.next;
temp=null;
return head;
}
}
}
Comments
Post a Comment