-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdel_a_node.cpp
More file actions
43 lines (38 loc) · 896 Bytes
/
del_a_node.cpp
File metadata and controls
43 lines (38 loc) · 896 Bytes
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
/*
* Complete the 'deleteNode' function below.
*
* The function is expected to return an INTEGER_SINGLY_LINKED_LIST.
* The function accepts following parameters:
* 1. INTEGER_SINGLY_LINKED_LIST llist
* 2. INTEGER position
*/
/*
* For your reference:
*
* SinglyLinkedListNode {
* int data;
* SinglyLinkedListNode* next;
* };
*
*/
SinglyLinkedListNode* deleteNode(SinglyLinkedListNode* llist, int position) {
int n = 1;
if(position == 0) {
SinglyLinkedListNode* t = llist;
llist = llist -> next;
free(t);
} else {
SinglyLinkedListNode* temp = llist;
SinglyLinkedListNode* ptr = llist;
while(temp != 0 and n < position) {
temp = temp -> next;
ptr = ptr -> next;
n++;
}
ptr = ptr -> next;
temp -> next = ptr -> next;
ptr -> next = NULL;
free(ptr);
}
return llist;
}