-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay_67.cpp
49 lines (39 loc) · 1.23 KB
/
Day_67.cpp
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
/*
DAY 67 : Delete Middle of Linked List.
https://www.geeksforgeeks.org/delete-middle-of-linked-list/
QUESTION : Given a singly linked list, delete middle of the linked list. For example,
if given linked list is 1->2->3->4->5 then linked list should be modified to 1->2->4->5.
If there are even nodes, then there would be two middle nodes, we need to delete the
second middle element. For example, if given linked list is 1->2->3->4->5->6 then it
should be modified to 1->2->3->5->6.
If the input linked list is NULL or has 1 node, then it should return NULL.
Expected Time Complexity : O(N)
Expected Auxilliary Space : O(1)
Constraints:
1 <= N <= 1000
1 <= value <= 1000
*/
Node* deleteMid(Node* head)
{
// Your Code Here
int count = 0;
if (head == NULL) {
return NULL;
}
Node* current = head;
while(current != NULL) {
current = current->next;
count++;
}
int mid = count/2 + 1;
current = head;
count = 0;
while(current->next != NULL) {
count++;
if (count==mid-1) {
current->next = current->next->next;
return head;
}
current = current->next;
}
}