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,
- Leetcode Remove Nth Node From End of List problem solution in C++
- Leetcode Remove Nth Node From End of List problem solution in Java
- Leetcode Remove Nth Node From End of List problem solution in Python
- Leetcode Remove Nth Node From End of List problem solution in C#