Skip to content

Commit 232e05c

Browse files
authored
Update Remove_all_Duplicates_from_Linnkedlist.cpp
Comments added
1 parent 55163b1 commit 232e05c

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

Data Structures/Linked Lists/Remove_all_Duplicates_from_Linnkedlist.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ Answer : 1 2 3
1010
#include <bits/stdc++.h>
1111
using namespace std;
1212

13+
// A linked list node
1314
class Node
1415
{
1516
public:
1617
int data;
1718
Node *next;
1819
};
20+
21+
// Utility function to create a new Node
1922
void push(Node **head, int n_data)
2023
{
2124
Node *newdata = new Node();
2225
newdata->data = n_data;
2326
newdata->next = (*head);
2427
(*head) = newdata;
2528
}
29+
30+
// Function to print nodes in a given linked list.
2631
void printlinkedlist(Node *temp)
2732
{
2833
while (temp != NULL)
@@ -31,17 +36,27 @@ void printlinkedlist(Node *temp)
3136
temp = temp->next;
3237
}
3338
}
39+
40+
// Function to delete duplicate nodes
3441
Node *delete_duplicates(Node *head)
3542
{
43+
//initalize current to head
3644
Node *curr = head;
45+
// when curr and curr->next is not NULL
46+
// enter the loop
3747
while (curr && curr->next)
3848
{
49+
// if the adjacent nodes are found to be equal
3950
if (curr->next->data == curr->data)
4051
{
52+
// then point the curr->next to one step ahead
53+
// to the existing pointer.
4154
curr->next = curr->next->next;
4255
}
4356
else
4457
{
58+
// Until the current and current->next values are
59+
// not same, keep updating current
4560
curr = curr->next;
4661
}
4762
}
@@ -80,4 +95,4 @@ New Linked List:
8095
8196
Time-Complexity: O(n)
8297
Space-Complexity: O(1)
83-
*/
98+
*/

0 commit comments

Comments
 (0)