File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Data Structures/Linked Lists Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -10,19 +10,24 @@ Answer : 1 2 3
10
10
#include < bits/stdc++.h>
11
11
using namespace std ;
12
12
13
+ // A linked list node
13
14
class Node
14
15
{
15
16
public:
16
17
int data;
17
18
Node *next;
18
19
};
20
+
21
+ // Utility function to create a new Node
19
22
void push (Node **head, int n_data)
20
23
{
21
24
Node *newdata = new Node ();
22
25
newdata->data = n_data;
23
26
newdata->next = (*head);
24
27
(*head) = newdata;
25
28
}
29
+
30
+ // Function to print nodes in a given linked list.
26
31
void printlinkedlist (Node *temp)
27
32
{
28
33
while (temp != NULL )
@@ -31,17 +36,27 @@ void printlinkedlist(Node *temp)
31
36
temp = temp->next ;
32
37
}
33
38
}
39
+
40
+ // Function to delete duplicate nodes
34
41
Node *delete_duplicates (Node *head)
35
42
{
43
+ // initalize current to head
36
44
Node *curr = head;
45
+ // when curr and curr->next is not NULL
46
+ // enter the loop
37
47
while (curr && curr->next )
38
48
{
49
+ // if the adjacent nodes are found to be equal
39
50
if (curr->next ->data == curr->data )
40
51
{
52
+ // then point the curr->next to one step ahead
53
+ // to the existing pointer.
41
54
curr->next = curr->next ->next ;
42
55
}
43
56
else
44
57
{
58
+ // Until the current and current->next values are
59
+ // not same, keep updating current
45
60
curr = curr->next ;
46
61
}
47
62
}
@@ -80,4 +95,4 @@ New Linked List:
80
95
81
96
Time-Complexity: O(n)
82
97
Space-Complexity: O(1)
83
- */
98
+ */
You can’t perform that action at this time.
0 commit comments