Leetcode Remove Nth Node From End of List problem solution in C programming

In the Leetcode Remove Nth Node From End of List problem solution in C programming Given the head of a linked list, remove the nth node from the end of the list and return its head.

Leetcode Remove Nth Node From End of List problem solution in C programming

struct ListNode* removeNthFromEnd(struct ListNode* head, int n){
    if(!head) return NULL;
    int count = 0;
    struct ListNode *temp = head;
    while(temp != NULL){
        count += 1;
        temp = temp -> next;
    }
    if(count <= 1 && n <= 1){
        return NULL;
    }
    else if(count == n){
        return head -> next;
    }
    count -= n;
    int i = 1;
    temp = head;
    while(i < count){
        temp = temp -> next;
        i += 1;
    }
    temp -> next = temp ->next -> next;
    
    return head;
}

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *