-
Notifications
You must be signed in to change notification settings - Fork 2
/
question11.c
65 lines (55 loc) · 1.46 KB
/
question11.c
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
/*
Alternating split of a given linked list.
We can use a counter a split at even and odd numbers and move them to new list, but then we have
dont have to create any new list but use the existing one and split it
METHOD:
take two pointers one pointing to head and one to head->next, then keep assigning them the next
node as per the pattern until one of them reaches null. Make the other one also null in the end
Time complexity: O(n)
Space complexity: O(1)
*/
//METHOD
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
void makeList(struct node *t, int maxCounter, int mul){
int counter = 1;
while(counter <=maxCounter){
t->data = counter*mul;
if(counter == maxCounter){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));;
}
t = t->link;
counter++;
}
}
void printList(struct node *head){
if(head){
printf("%d-->", head->data);
printList(head->link);
}
printf("\n");
}
void alternateList(struct node *head1, struct node *head2){
for(;head1 && head2 && head1->link && head2->link && head2->link->link;
head1=head1->link, head2=head2->link){
head1->link = head2->link;
head2->link = head2->link->link;
}
head1->link = head2->link = NULL;
}
int main(){
struct node *head1 = (struct node *)malloc(sizeof(struct node));
struct node *t = head1;
makeList(t,8,10);
struct node *head2 = head1->link;
printList(head1);
alternateList(head1,head2);
printList(head1);
printList(head2);
}