-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedlistRemove.c
90 lines (75 loc) · 2.19 KB
/
LinkedlistRemove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//QUESTION: Remove the N-th node from the end of a linked list.
//CODE:
#include <stdio.h>
#include <stdlib.h>
// Definition for singly-linked list.
struct ListNode {
int val;
struct ListNode *next;
};
// Function to create a new node
struct ListNode* createNode(int val) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
// Function to remove the N-th node from the end of the list
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
// Create a dummy node to simplify edge cases
struct ListNode* dummy = createNode(0);
dummy->next = head;
struct ListNode *first = dummy, *second = dummy;
// Move first pointer n+1 steps ahead
for (int i = 0; i <= n; i++) {
first = first->next;
}
// Move first to the end, maintaining the gap
while (first != NULL) {
first = first->next;
second = second->next;
}
// Remove the N-th node
struct ListNode* nodeToRemove = second->next;
second->next = nodeToRemove->next;
free(nodeToRemove);
// Get the new head
struct ListNode* newHead = dummy->next;
free(dummy);
return newHead;
}
// Function to print the linked list
void printList(struct ListNode* head) {
struct ListNode* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->val);
temp = temp->next;
}
printf("NULL\n");
}
// Function to free the linked list
void freeList(struct ListNode* head) {
struct ListNode* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
int main() {
// Creating a linked list for demonstration
struct ListNode* head = createNode(1);
head->next = createNode(2);
head->next->next = createNode(3);
head->next->next->next = createNode(4);
head->next->next->next->next = createNode(5);
printf("Original list:\n");
printList(head);
int n = 2; // Remove the 2nd node from the end
head = removeNthFromEnd(head, n);
printf("List after removing %d-th node from the end:\n", n);
printList(head);
// Free the list
freeList(head);
return 0;
}