Skip to content

Commit a7d8861

Browse files
authored
Create Delete alternate nodes
1 parent e56ebef commit a7d8861

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

Test/Delete alternate nodes

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
Given a Singly Linked List of integers, delete all the alternate nodes in the list.
2+
Example:
3+
List: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> null
4+
Alternate nodes will be: 20, 40, and 60.
5+
6+
Hence after deleting, the list will be:
7+
Output: 10 -> 30 -> 50 -> null
8+
9+
10+
void delete_alternate_node_LinkedList(Node *head) {
11+
12+
if(head==NULL)
13+
return;
14+
if(head->next==NULL)
15+
return;
16+
17+
int x=length(head);
18+
19+
Node *p=head;
20+
Node *q=head;
21+
22+
for(int i=2 ; i <x ; i=i+2)
23+
{ p=p->next;
24+
Node *t=p;
25+
p=p->next;
26+
q->next=p;
27+
delete t;
28+
q=p;
29+
}
30+
31+
if(x%2==0)
32+
{
33+
p=p->next;
34+
q->next=NULL;
35+
delete p;
36+
37+
}
38+
39+
}

0 commit comments

Comments
 (0)