Skip to content

Commit

Permalink
Adds circular list insert at tail, delete head and delete at position…
Browse files Browse the repository at this point in the history
… k solutions
  • Loading branch information
omonimus1 committed Jun 23, 2020
1 parent aa085ec commit be23611
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ My solutions to competitive programming problems in [Geeks for Geeks](https://au
|Display circular linked list|[C++](c++/display-circular-linked-list.cpp)|
|Insert at tail (end) of circular linked list|[C++](c++/insert-tail-circular-linked-list.cpp)|
|Get length(number of nodes) of a circualr linke list|[C++](c++/get-length-circular-list.cpp)|
| Delete head of a circular list |[C++](c++/delete-head-of-circular-list.cpp)|
|Insert at head in a **circular linked list**|[C++](c++/circular-linked-list-head-insert.cpp)|
|Insert at **end** of a **circular linked list**|[C++](c++/insert-at-the-end-of-circular-linked-list.cpp)|
|Delete node of Circular list at a specific position|[C++](c++/circular-list-delete-at-position.cpp)|
|Max and minimum in linked list|[C++](c++/maximum-and-minimum-in-linked-list.cpp)|
|Delete tail of linked list|[C++](c++/detele-tail-linked-list.cpp)|
|Search in Linked list|[C++](c++/search-in-linked-list.cpp)|
Expand Down
13 changes: 11 additions & 2 deletions c++/circular-linked-list-head-insert.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
// https://practice.geeksforgeeks.org/problems/circular-linked-list-head-insert/1/?track=DSA-Foundation-Final-Circular-Linked-List&batchId=193
// Time complexity: O(N)
// Space complexity: O(1)
/*
If we need to optimize this code and do it in
O(1) time, we can simply keep stored the updated value of pointer
to the tail node, so that we do not need to iterate every time the
Circular Linked List N times.
*/
Node *insertInHead(Node * head, int data)
{
Node *new_node = new Node(data);
// Corner case: Linked list is empty
if(head == NULL)
new_node ->next = new_node;
else
{
Node *current = head;
while(current->next != head)
{
current = current->next;
}
// Set last node's next pointing to the new head
current->next = new_node;
new_node ->next = head;
}
Expand Down
23 changes: 23 additions & 0 deletions c++/circular-list-delete-at-position.cpp
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;
}
}
56 changes: 56 additions & 0 deletions c++/delete-head-of-circular-list.cpp
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;
}
4 changes: 4 additions & 0 deletions c++/insert-at-the-end-of-circular-linked-list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//
/*
De
*/

0 comments on commit be23611

Please sign in to comment.