Skip to content

Commit 512f06e

Browse files
authored
Merge pull request #583 from Amitesh-Patel/main
Added Remove Duplicate in Linked List and Reverse
2 parents c9aae04 + 1d14c94 commit 512f06e

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
struct Node{
6+
int data;
7+
struct Node *next;
8+
}*first=NULL,*second=NULL,*third=NULL;
9+
10+
11+
void Display(struct Node *p){
12+
while(p!=NULL){
13+
printf("%d ",p->data);
14+
p=p->next;
15+
}
16+
}
17+
18+
void create(int A[],int n){
19+
int i;
20+
struct Node *t,*last;
21+
first=(struct Node *)malloc(sizeof(struct Node));
22+
first->data=A[0];
23+
first->next=NULL;
24+
last=first;
25+
for(i=1;i<n;i++){
26+
t=(struct Node*)malloc(sizeof(struct Node));
27+
t->data=A[i];
28+
t->next=NULL;
29+
last->next=t;
30+
last=t;
31+
}
32+
}
33+
34+
void RemoveDuplicate(struct Node *p){
35+
struct Node *q=p->next;
36+
37+
while(q!=NULL){
38+
if(p->data!=q->data){
39+
p=q;
40+
q=q->next;
41+
}
42+
else
43+
{
44+
p->next=q->next;
45+
free(q);
46+
q=p->next;
47+
}
48+
}
49+
}
50+
51+
//count
52+
int count(struct Node *p){
53+
int cnt=0;
54+
while(p){
55+
cnt++;
56+
p=p->next;
57+
}
58+
return cnt;
59+
}
60+
61+
//reversing a linked list
62+
void Reverse1(struct Node *p){
63+
int *A,i=0;
64+
struct Node *q=p;
65+
66+
A=(int *)malloc(sizeof(int)*count(p));
67+
while(q!=NULL){
68+
A[i]=q->data;
69+
q=q->next;
70+
i++;
71+
}
72+
q=p;
73+
i--;
74+
while(q!=NULL){
75+
q->data=A[i];
76+
q=q->next;
77+
i--;
78+
}
79+
}
80+
81+
82+
void Reverse2(struct Node *p){
83+
struct Node *q=NULL,*r=NULL;
84+
while(p!=NULL){
85+
r=q;
86+
q=p;
87+
p=p->next;
88+
q->next=r;
89+
}
90+
first=q;
91+
}
92+
93+
94+
void Reverse3(struct Node *q,struct Node *p){
95+
if(p){
96+
Reverse3(p,p->next);
97+
p->next=q;
98+
}
99+
else
100+
first=q;
101+
}
102+
103+
int main()
104+
{
105+
int A[]={10,20,20,40,50,50,50,60};
106+
create(A,8);
107+
count(first);
108+
RemoveDuplicate(first);
109+
Display(first);
110+
Reverse2(first);
111+
Display(first);
112+
return 0;
113+
}

0 commit comments

Comments
 (0)