1
+ {
2
+ "nbformat" : 4 ,
3
+ "nbformat_minor" : 0 ,
4
+ "metadata" : {
5
+ "colab" : {
6
+ "name" : " Test-2" ,
7
+ "provenance" : [],
8
+ "collapsed_sections" : []
9
+ },
10
+ "kernelspec" : {
11
+ "name" : " python3" ,
12
+ "display_name" : " Python 3"
13
+ },
14
+ "language_info" : {
15
+ "name" : " python"
16
+ }
17
+ },
18
+ "cells" : [
19
+ {
20
+ "cell_type" : " code" ,
21
+ "execution_count" : null ,
22
+ "metadata" : {
23
+ "id" : " xXyruCkAr700"
24
+ },
25
+ "outputs" : [],
26
+ "source" : [
27
+ " '''\n " ,
28
+ " Next Number\n " ,
29
+ " \n " ,
30
+ " Given a large number represented in the form of a linked list. Write code to increment the \n " ,
31
+ " number by 1 in-place(i.e. without using extra space).\n " ,
32
+ " \n " ,
33
+ " Note: You don't need to print the elements, just update the elements and return the head of updated LL.\n " ,
34
+ " \n " ,
35
+ " Input Constraints:\n " ,
36
+ " 1 <= Length of Linked List <=10^6.\n " ,
37
+ " \n " ,
38
+ " Input format :\n " ,
39
+ " Line 1 : Linked list elements (separated by space and terminated by -1)\n " ,
40
+ " \n " ,
41
+ " Output Format :\n " ,
42
+ " Line 1: Updated linked list elements \n " ,
43
+ " \n " ,
44
+ " Sample Input 1 :\n " ,
45
+ " 3 9 2 5 -1\n " ,
46
+ " Sample Output 1 :\n " ,
47
+ " 3 9 2 6\n " ,
48
+ " \n " ,
49
+ " Sample Input 2 :\n " ,
50
+ " 9 9 9 -1\n " ,
51
+ " Sample Output 1 :\n " ,
52
+ " 1 0 0 0 \n " ,
53
+ " '''\n " ,
54
+ " \n " ,
55
+ " class Node:\n " ,
56
+ " def __init__(self, data):\n " ,
57
+ " self.data = data\n " ,
58
+ " self.next = None\n " ,
59
+ " \n " ,
60
+ " def reverseLL(head):\n " ,
61
+ " prev = None\n " ,
62
+ " current = head\n " ,
63
+ " while(current is not None):\n " ,
64
+ " next = current.next\n " ,
65
+ " current.next = prev\n " ,
66
+ " prev = current\n " ,
67
+ " current = next\n " ,
68
+ " return prev\n " ,
69
+ " \n " ,
70
+ " def nextNumber(head):\n " ,
71
+ " head = reverseLL(head)\n " ,
72
+ " carry = 1\n " ,
73
+ " temp = head\n " ,
74
+ " while(temp is not None):\n " ,
75
+ " sum = temp.data + carry\n " ,
76
+ " temp.data = sum%10\n " ,
77
+ " carry = sum//10\n " ,
78
+ " temp = temp.next\n " ,
79
+ " \n " ,
80
+ " if carry==1:\n " ,
81
+ " temp = head\n " ,
82
+ " while(temp.next is not None):\n " ,
83
+ " temp = temp.next\n " ,
84
+ " newNode = Node(carry)\n " ,
85
+ " temp.next = newNode\n " ,
86
+ " \n " ,
87
+ " return reverseLL(head)\n " ,
88
+ " \n " ,
89
+ " \n " ,
90
+ " def ll(arr):\n " ,
91
+ " if len(arr)==0:\n " ,
92
+ " return None\n " ,
93
+ " head = Node(arr[0])\n " ,
94
+ " last = head\n " ,
95
+ " for data in arr[1:]:\n " ,
96
+ " last.next = Node(data)\n " ,
97
+ " last = last.next\n " ,
98
+ " return head\n " ,
99
+ " \n " ,
100
+ " def printll(head):\n " ,
101
+ " while head is not None:\n " ,
102
+ " print(head.data,end= ' ')\n " ,
103
+ " head = head.next\n " ,
104
+ " return\n " ,
105
+ " \n " ,
106
+ " # Read the link list elements including -1\n " ,
107
+ " arr=[int(ele) for ele in input().split()]\n " ,
108
+ " # Create a Linked list after removing -1 from list\n " ,
109
+ " l = ll(arr[:-1])\n " ,
110
+ " head = nextNumber(l)\n " ,
111
+ " printll(head)"
112
+ ]
113
+ },
114
+ {
115
+ "cell_type" : " code" ,
116
+ "source" : [
117
+ " '''\n " ,
118
+ " Dequeue\n " ,
119
+ " \n " ,
120
+ " You need to implement a class for Dequeue i.e. for double ended queue. In this queue, elements can be \n " ,
121
+ " inserted and deleted from both the ends.\n " ,
122
+ " \n " ,
123
+ " You don't need to double the capacity.\n " ,
124
+ " \n " ,
125
+ " You need to implement the following functions -\n " ,
126
+ " \n " ,
127
+ " 1. constructor - You need to create the appropriate constructor. Size for the queue passed is 10.\n " ,
128
+ " 2. insertFront - This function takes an element as input and insert the element at the front of queue. \n " ,
129
+ " Insert the element only if queue is not full. And if queue is full, print -1 and return.\n " ,
130
+ " 3. insertRear - This function takes an element as input and insert the element at the end of queue. \n " ,
131
+ " Insert the element only if queue is not full. And if queue is full, print -1 and return.\n " ,
132
+ " 4. deleteFront - This function removes an element from the front of queue. Print -1 if queue is empty.\n " ,
133
+ " 5. deleteRear - This function removes an element from the end of queue. Print -1 if queue is empty.\n " ,
134
+ " 6. getFront - Returns the element which is at front of the queue. Return -1 if queue is empty.\n " ,
135
+ " 7. getRear - Returns the element which is at end of the queue. Return -1 if queue is empty.\n " ,
136
+ " \n " ,
137
+ " Sample Input 1:\n " ,
138
+ " 5\n " ,
139
+ " 1\n " ,
140
+ " 49\n " ,
141
+ " 1\n " ,
142
+ " 64\n " ,
143
+ " 2\n " ,
144
+ " 99\n " ,
145
+ " 5\n " ,
146
+ " 6\n " ,
147
+ " -1\n " ,
148
+ " \n " ,
149
+ " Sample Output 1:\n " ,
150
+ " -1\n " ,
151
+ " 64\n " ,
152
+ " 99\n " ,
153
+ " \n " ,
154
+ " Explanation:\n " ,
155
+ " The first choice code corresponds to getFront. Since the queue is empty, hence the output is -1. \n " ,
156
+ " The following input adds 49 at the top and the resultant queue becomes: 49.\n " ,
157
+ " The following input adds 64 at the top and the resultant queue becomes: 64 -> 49\n " ,
158
+ " The following input add 99 at the end and the resultant queue becomes: 64 -> 49 -> 99\n " ,
159
+ " The following input corresponds to getFront. Hence the output is 64.\n " ,
160
+ " The following input corresponds to getRear. Hence the output is 99.\n " ,
161
+ " '''\n " ,
162
+ " \n " ,
163
+ " import collections\n " ,
164
+ " inp_ls = list(map(int, input().split()))\n " ,
165
+ " itr = 0\n " ,
166
+ " queue = collections.deque()\n " ,
167
+ " n = 0\n " ,
168
+ " while inp_ls[itr]!=-1:\n " ,
169
+ " choice = inp_ls[itr]\n " ,
170
+ " if(choice==1):\n " ,
171
+ " itr = itr+1\n " ,
172
+ " if(n<=9):\n " ,
173
+ " queue.appendleft(inp_ls[itr])\n " ,
174
+ " n += 1\n " ,
175
+ " else:\n " ,
176
+ " print(-1)\n " ,
177
+ " elif(choice==2):\n " ,
178
+ " itr = itr+1\n " ,
179
+ " if(n<=9):\n " ,
180
+ " queue.append(inp_ls[itr])\n " ,
181
+ " n += 1\n " ,
182
+ " else:\n " ,
183
+ " print(-1)\n " ,
184
+ " elif(choice==3):\n " ,
185
+ " if(n>=1):\n " ,
186
+ " queue.popleft()\n " ,
187
+ " n -= 1\n " ,
188
+ " else:\n " ,
189
+ " print(-1)\n " ,
190
+ " elif(choice==4):\n " ,
191
+ " if(n>=1):\n " ,
192
+ " queue.pop()\n " ,
193
+ " n -= 1\n " ,
194
+ " else:\n " ,
195
+ " print(-1)\n " ,
196
+ " elif(choice==5):\n " ,
197
+ " if(n>=1):\n " ,
198
+ " element = queue.popleft()\n " ,
199
+ " print(element)\n " ,
200
+ " queue.appendleft(element)\n " ,
201
+ " else:\n " ,
202
+ " print(-1)\n " ,
203
+ " elif(choice==6):\n " ,
204
+ " if(n>=1):\n " ,
205
+ " element = queue.pop()\n " ,
206
+ " print(element)\n " ,
207
+ " queue.append(element)\n " ,
208
+ " else:\n " ,
209
+ " print(-1)\n " ,
210
+ " itr =itr + 1"
211
+ ],
212
+ "metadata" : {
213
+ "id" : " XCfBRa2RskmD"
214
+ },
215
+ "execution_count" : null ,
216
+ "outputs" : []
217
+ },
218
+ {
219
+ "cell_type" : " code" ,
220
+ "source" : [
221
+ " '''\n " ,
222
+ " Delete Alternate Nodes\n " ,
223
+ " \n " ,
224
+ " Given a Singly Linked List of integers, delete all the alternate nodes in the list.\n " ,
225
+ " \n " ,
226
+ " Example:\n " ,
227
+ " List: 10 -> 20 -> 30 -> 40 -> 50 -> 60 -> null\n " ,
228
+ " Alternate nodes will be: 20, 40, and 60.\n " ,
229
+ " \n " ,
230
+ " Hence after deleting, the list will be:\n " ,
231
+ " Output: 10 -> 30 -> 50 -> null\n " ,
232
+ " \n " ,
233
+ " Note :\n " ,
234
+ " The head of the list will remain the same. Don't need to print or return anything.\n " ,
235
+ " \n " ,
236
+ " Input format :\n " ,
237
+ " The first and the only line of input will contain the elements of the Singly Linked List separated by \n " ,
238
+ " a single space and terminated by -1.\n " ,
239
+ " \n " ,
240
+ " Output Format :\n " ,
241
+ " The only line of output will contain the updated list elements.\n " ,
242
+ " \n " ,
243
+ " Input Constraints:\n " ,
244
+ " 1 <= N <= 10 ^ 6.\n " ,
245
+ " Where N is the size of the Singly Linked List\n " ,
246
+ " \n " ,
247
+ " Time Limit: 1 sec\n " ,
248
+ " \n " ,
249
+ " Sample Input 1:\n " ,
250
+ " 1 2 3 4 5 -1\n " ,
251
+ " Sample Output 1:\n " ,
252
+ " 1 3 5\n " ,
253
+ " Explanation of Sample Input 1:\n " ,
254
+ " 2, 4 are alternate nodes so we need to delete them \n " ,
255
+ " \n " ,
256
+ " Sample Input 2:\n " ,
257
+ " 10 20 30 40 50 60 70 -1\n " ,
258
+ " Sample Output 2:\n " ,
259
+ " 10 30 50 70 \n " ,
260
+ " '''\n " ,
261
+ " \n " ,
262
+ " import math\n " ,
263
+ " \n " ,
264
+ " class Node:\n " ,
265
+ " \t def _init_(self, data):\n " ,
266
+ " \t\t self.data = data\n " ,
267
+ " \t\t self.next = None\n " ,
268
+ " \n " ,
269
+ " def deleteAlternateNodes(head):\n " ,
270
+ " \t if (head == None):\n " ,
271
+ " \t\t return\n " ,
272
+ " \t prev = head\n " ,
273
+ " \t now = head.next\n " ,
274
+ " \t while (prev != None and now != None):\n " ,
275
+ " \t\t prev.next = now.next\n " ,
276
+ " \t\t now = None\n " ,
277
+ " \t\t prev = prev.next\n " ,
278
+ " \t\t if (prev != None):\n " ,
279
+ " \t\t\t now = prev.next"
280
+ ],
281
+ "metadata" : {
282
+ "id" : " wlpObXDts-f8"
283
+ },
284
+ "execution_count" : null ,
285
+ "outputs" : []
286
+ }
287
+ ]
288
+ }
0 commit comments