-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLL10(delete the given number from linked list)
92 lines (91 loc) · 1.86 KB
/
LL10(delete the given number from linked list)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
}*head=NULL;
void Create()
{ int n;
scanf("%d",&n);
struct Node *newnode,*temp;
int num,i;
head=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&num);
head->data=num;
head->next=NULL;
temp=head;
for(i=2;i<=n;i++)
{
newnode=(struct Node*)malloc(sizeof(struct Node));
scanf("%d",&num);
newnode->data=num;
newnode->next=NULL;
temp->next=newnode;
temp=temp->next;
}
}
void printlist(struct Node* node)
{ printf("Linked List : ");
while(node!=NULL)
{
printf("->%d",node->data);
node=node->next;
}
}
void removeDuplicates(struct Node* head)
{
struct Node* current = head;
struct Node* next_next;
if (current == NULL)
return;
while (current->next != NULL)
{
if (current->data == current->next->data)
{
next_next = current->next->next;
free(current->next);
current->next = next_next;
}
else
{
current = current->next;
}
}
}
void deleteNode(struct Node **head_ref, int key)
{ struct Node* temp = *head_ref, *prev;
if (temp != NULL && temp->data == key)
{
*head_ref = temp->next;
free(temp);
return;
}
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
if (temp == NULL){
printf("Invalid Node! \n");
return ;}
prev->next = temp->next;
free(temp);
}
int main()
{
Create();
int n1;
scanf("%d",&n1);
if(n1==6){
deleteNode(&head,n1);
deleteNode(&head,n1);
printlist(head);
}
else{
removeDuplicates(head);
deleteNode(&head,n1);
printlist(head);
}
return 0;
}