-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion17.c
98 lines (83 loc) · 1.9 KB
/
question17.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
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
93
94
95
96
97
98
/*
Consider a single linked list with each node having an arbitrary pointer with
initial value NULL.Give an algorithm to make the arbitrary pointer point to the greatest value node on
its right side.
METHOD1:
Naive approach where you compare each element to right of it find max for each and make arb point towards it.
Time complexity: O(n^2)
Space complexity: O(1)
METHOD2:
Reverse the list.
Find max element towards left and make arb pointer point towards it in one traversal only.
Reverse again
Time complexity: O(n)
Space complexity: O(1)
*/
//METHOD2
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *arb;
struct node *link;
};
void printList(struct node *head){
for(;head;head=head->link){
if(head->arb){
printf("%d %d --> ", head->data, head->arb->data);
}else{
printf("%d %s --> ", head->data, "null");
}
}
printf("\n");
}
void makeList(struct node *head){
int count = 1;
while(count <= 5){
head->data = count*10;
head->arb = NULL;
if(count == 5){
head->link = NULL;
}else{
head->link = (struct node *)malloc(sizeof(struct node));
}
head = head->link;
count++;
}
}
struct node *reverseList(struct node *head){
struct node *prev = NULL, *curr = head, *next;
while(curr){
next = curr->link;
curr->link = prev;
prev = curr;
curr = next;
}
head = prev;
return head;
}
struct node *getMax(struct node *curr_max, struct node *prev){
if(!curr_max){
return (!prev)?NULL: prev;
}
return (curr_max->data > prev->data)? curr_max: prev;
}
void assignArb(struct node *head){
head = reverseList(head);
struct node *t = head;
struct node *curr_max = NULL;
struct node *prev = NULL;
for(;t;t=t->link){
curr_max = getMax(curr_max, prev);
t->arb = curr_max;
prev = t;
}
head = reverseList(head);
}
int main(){
struct node *head = (struct node *)malloc(sizeof(struct node));
makeList(head);
assignArb(head);
printList(head);
return 0;
}