Remove Nth Node From End of List
C
1#include<stdio.h>
2struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
3 int count = 0;
4 struct ListNode * temp = head;
5 while(temp != NULL){
6 temp = temp->next;
7 count++;
8 }
9 if (count == 1 && n == 1){
10 return NULL;
11 }
12 count = (count - n);
13 temp = head;
14 while(count != 0){
15 temp = temp->next;
16 count--;
17 }
18 if(temp == head && temp->next != NULL){
19 return temp->next;
20 }
21 struct ListNode * prev_temp = head;
22 while(prev_temp->next != temp){
23 prev_temp = prev_temp->next;
24 }
25 prev_temp->next = temp->next;
26 return head;
27}