-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds circular list insert at tail, delete head and delete at position…
… k solutions
- Loading branch information
Showing
5 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// https://practice.geeksforgeeks.org/problems/circular-linked-list-delete-at-position/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193 | ||
|
||
Node * deleteAtPosition(Node *head,int position) | ||
{ | ||
if(head == NULL) | ||
return NULL; | ||
// Corner case: the node to remove is the head | ||
if(position == 1) | ||
{ | ||
return free(head);; | ||
} | ||
else | ||
{ | ||
Node *current = head; | ||
for(int i = 0; i < position-2; i++) | ||
current = current ->next; | ||
|
||
Node *temp = current->next; | ||
current->next = current->next->next; | ||
free(temp); | ||
return head; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// | ||
/* | ||
De | ||
*/ |