-
Notifications
You must be signed in to change notification settings - Fork 70
/
delete-head-of-circular-list.cpp
56 lines (50 loc) · 1.28 KB
/
delete-head-of-circular-list.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
50
51
52
53
54
55
56
/*
Delete head of a circular linked List
This implementation is in O(N) time;
We can actually delete the head in O(1) time.
To do this operation in O(1) time, we have to:
- Store second nodes value to in the head
- Delete second node of the linked list
*/
// O(N) time
Node * deleteHead(Node *head)
{
if (head == NULL)
return NULL;
if(head ->next == NULL)
{
free(head);
return NULL;
}
else
{
Node *current = head;
while(current -> next != head)
current = current->next;
// Set next pointer of the tail to the second node of the linked list
current->next = head->next;
// Remove original head of the Circular Linked list
head->next = NULL;
free(head);
// Return pointer to the second node of the original linked list(new head);
return current->next;
}
}
// O(1) Time
Node * deleteHead(Node *head)
{
if (head == NULL)
return NULL;
if(head ->next == NULL)
{
free(head);
return NULL;
}
// Store second nodes value in the head
head->data = head->next->data;
Node *temp = head->next;
head->next = head->next->next;
// Remove secon node from linked list
free(temp);
return head;
}