-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathLinkedList.py
773 lines (654 loc) · 19.6 KB
/
LinkedList.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
''McPupreme''
#8440
•.¸¸.•````•.¸¸.•````•.¸¸.•
Habibi Cat — Today at 1:15 AM
😂
koi api use karoonga
suggest kari
''McPupreme'' — Today at 1:16 AM
Ohhh
api ka nhi pta merko
extension kis cheez ki banayega
Habibi Cat — Today at 1:17 AM
soch raha tha 2-3 cheeze
dimmag me hai idea
but plan nahi hai kaise execute hoga
web scrapping
ml
react se
''McPupreme'' — Today at 1:19 AM
Ohhh
Habibi Cat — Today at 1:19 AM
aise 2-3 idea the
''McPupreme'' — Today at 1:19 AM
acha
karo bhaiya karo
:okbro:
Habibi Cat — Today at 1:19 AM
inme se koi ek
PutaTramcker
BOT
— Today at 1:19 AM
Item added
Nike Men Red Solid Dunk High SE Leather Sneakers is now being tracked ✅ @''McPupreme''
Image
Habibi Cat — Today at 1:19 AM
karte kaise hai voh seekh raha
''McPupreme'' — Today at 1:19 AM
Myntra bhi add hogya @Abhishek ✅
Tata cliq possible nahi lag rha abhi🥲
Habibi Cat — Today at 1:20 AM
:cylindermaaru:
''McPupreme'' — Today at 1:21 AM
Terko selenium aata h? @Habibi Cat
:clowncat:
Habibi Cat — Today at 1:22 AM
nahi
''McPupreme'' — Today at 1:22 AM
aati to thi🥲
Habibi Cat — Today at 1:22 AM
tabhi tih bola dekh raha hoon
thodi si
''McPupreme'' — Today at 1:22 AM
:okbro: :okbro:
Habibi Cat — Today at 1:22 AM
🥺
''McPupreme'' — Today at 1:22 AM
Ohk
Habibi Cat — Today at 1:22 AM
itne ache se nahi aati
''McPupreme'' — Today at 1:24 AM
Thodi toh aati h na
💀
Habibi Cat — Today at 1:26 AM
😂
whatsapp dekh
''McPupreme'' — Today at 1:28 AM
Hugger avi op
Habibi Cat — Today at 11:22 AM
Image
''McPupreme'' — Today at 11:27 AM
https://github.com/agampunchhi/DiscordJSBot
GitHub
GitHub - agampunchhi/DiscordJSBot
Contribute to agampunchhi/DiscordJSBot development by creating an account on GitHub.
Raptor — Today at 11:41 AM
https://github.com/CaZ-dev/HattjaHattja-Bot
GitHub
GitHub - CaZ-dev/HattjaHattja-Bot
Contribute to CaZ-dev/HattjaHattja-Bot development by creating an account on GitHub.
Habibi Cat — Today at 11:56 AM
https://www.programiz.com/dsa/binary-search-tree
Binary Search Tree
A binary search tree is a data structure that quickly allows us to maintain a sorted list of numbers. Also, you will find working examples of Binary Search Tree in C, C++, Java, and Python.
https://codemyui.com/marquee-page-border-effect-on-scroll-using-gsap/
CodeMyUI
Hima Vincent
Marquee Page Border Effect On Scroll using GSAP
Code by: Ryan Mulligan
https://github.com/Dipanshu089/Scroll-Animation-
GitHub
GitHub - Dipanshu089/Scroll-Animation-: Page Border Effect On Scrol...
Page Border Effect On Scroll Using GSAP. Contribute to Dipanshu089/Scroll-Animation- development by creating an account on GitHub.
https://github.com/Dipanshu089/Scroll-Animation
GitHub
GitHub - Dipanshu089/Scroll-Animation: Page Border Effect On Scroll...
Page Border Effect On Scroll Using GSAP. Contribute to Dipanshu089/Scroll-Animation development by creating an account on GitHub.
''McPupreme'' — Today at 12:06 PM
Image
@Habibi Cat
Habibi Cat — Today at 12:34 PM
#Power Of A Number
"""
Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer.
Note : For this question, you can assume that 0 raised to the power of 0 is 1
Input format :
Expand
1._Recursion_-_1.py
4 KB
#Power Of A Number
"""
Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to return the answer.
Do this recursively.
Input format :
Expand
4._Time_Complexity_Improvement.py
9 KB
https://github.com/dipanshu0892/Data-Structures-Algo-with-Python
GitHub
GitHub - dipanshu0892/Data-Structures-Algo-with-Python
Contribute to dipanshu0892/Data-Structures-Algo-with-Python development by creating an account on GitHub.
#Length of LL
"""
For a given singly linked list of integers, find and return its length. Do it using an iterative method.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Expand
5._Linked_List-1.py
17 KB
#Length of LL
"""
For a given singly linked list of integers, find and return its length. Do it using an iterative method.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Then the test cases follow.
First and the only line of each test case or query contains elements of the singly linked list separated by a single space.
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence,
would never be a list element.
Output format :
For each test case, print the length of the linked list.
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
1
3 4 5 2 6 1 9 -1
Sample Output 1 :
7
Sample Input 2 :
2
10 76 39 -3 2 9 -23 9 -1
-1
Sample Output 2 :
8
0
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def findlength(head):
if head == None:
return 0
if head.next == None:
return 1
return findlength(head.next) + 1
def ll(arr):
if arr == []:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
from sys import setrecursionlimit
setrecursionlimit(11000)
arr = list(int(i) for i in input().strip().split(' '))
l = ll(arr[:-1])
len = findlength(l)
print(len)
#Print ith node
"""
For a given a singly linked list of integers and a position 'i', print the node data at the 'i-th' position.
Note :
Assume that the Indexing for the singly linked list always starts from 0.
If the given position 'i', is greater than the length of the given singly linked list, then don't print anything.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Then the test cases follow.
The first line of each test case or query contains the elements of the singly linked list separated by a single space.
The second line contains the value of 'i'. It denotes the position in the given singly linked list.
Remember/Consider :
While specifying the list elements for input,
-1 indicates the end of the singly linked list and hence, would never be a list element.
Output format :
For each test case, print the node data at the 'i-th' position of the linked list(if exists).
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
1
3 4 5 2 6 1 9 -1
3
Sample Output 1 :
2
Sample Input 2 :
2
3 4 5 2 6 1 9 -1
0
9 8 4 0 7 8 -1
3
Sample Output 2 :
3
0
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def ithNode(head, i):
count = 0
current = head
while count < i and current != None:
current = current.next
count = count + 1
return current
def ll(arr):
if arr == []:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
from sys import setrecursionlimit
setrecursionlimit(11000)
arr = list(int(i) for i in input().strip().split(' '))
i = int(input())
l = ll(arr[:-1])
node = ithNode(l, i)
if node:
print(node.data)
#Delete node
"""
You have been given a linked list of integers.
Your task is to write a function that deletes a node from a given position, 'pos'.\
Note :
Assume that the Indexing for the linked list always starts from 0.
If the position is greater than or equal to the length of the linked list,
you should return the same linked list without any change.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Then the test cases follow.
The first line of each test case or query contains the elements of the linked list separated by a single space.
The second line contains the integer value of 'pos'.
It denotes the position in the linked list from where the node has to be deleted.
Output format :
For each test case/query, print the resulting linked list of integers in a row, separated by a single space.
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
1
3 4 5 2 6 1 9 -1
3
Sample Output 1 :
3 4 5 6 1 9
Sample Input 2 :
2
3 4 5 2 6 1 9 -1
0
10 20 30 40 50 60 -1
7
Sample Output 2 :
4 5 2 6 1 9
10 20 30 40 50 60
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def deleteRec(head, i):
if (i < 0) :
return head
if (head == None):
return None
if (i == 0) :
res = head.next
return res
head.next = deleteRec(head.next, i-1 )
return head
pass
def ll(arr):
if len(arr)==0:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
def printll(head):
while head:
print(head.data, end=' ')
head = head.next
print()
from sys import setrecursionlimit
setrecursionlimit(11000)
arr=list(int(i) for i in input().strip().split(' '))
l = ll(arr[:-1])
i=int(input())
l = deleteRec(l, i)
printll(l)
#Find a Node in Linked List
"""
You have been given a singly linked list of integers.
Write a function that returns the index/position of an integer data denoted by 'N'(if it exists). -1 otherwise.
Note :
Assume that the Indexing for the singly linked list always starts from 0.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Then the test cases follow.
The first line of each test case or query contains the elements of the singly linked list separated by a single space.
The second line contains the integer value 'N'.
It denotes the data to be searched in the given singly linked list.
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence,
would never be a list element.
Output format :
For each test case/query, print the index/position of 'N' in the singly linked list. -1, otherwise.
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
2
3 4 5 2 6 1 9 -1
5
10 20 30 40 50 60 70 -1
6
Sample Output 1 :
2
-1
Sample Input 2 :
1
3 4 5 2 6 1 9 -1
6
Sample Output 2 :
4
Explanation for Sample Input 2 :
For the given singly linked list, considering the indices starting from 0,
progressing in a left to right manner with a jump of 1, then the N = 6 appears at position 4.
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def findNode(head,number, index):
if (head == None):
return -1
if (head.data == number) :
return index
return findNode(head.next,number, index+1)
def ll(arr):
if len(arr)==0:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
def printll(head):
while head:
print(head.data, end=' ')
head = head.next
print()
from sys import setrecursionlimit
setrecursionlimit(11000)
arr=list(int(i) for i in input().strip().split(' '))
l = ll(arr[:-1])
numberToFind=int(input())
index = findNode(l,numberToFind, 0)
print(index)
#AppendLastNToFirst
"""
You have been given a singly linked list of integers along with an integer 'N'.
Write a function to append the last 'N' nodes towards the front of the singly linked list and returns the new head to the list.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Then the test cases follow.
The first line of each test case or query contains the elements of the singly linked list separated by a single space.
The second line contains the integer value 'N'. It denotes the number of nodes to be moved from last to the front of the singly linked list.
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.
Output format :
For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space.
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
2
1 2 3 4 5 -1
3
10 20 30 40 50 60 -1
5
Sample Output 1 :
3 4 5 1 2
20 30 40 50 60 10
Sample Input 2 :
1
10 6 77 90 61 67 100 -1
4
Sample Output 2 :
90 61 67 100 10 6 77
Explanation to Sample Input 2 :
We have been required to move the last 4 nodes to the front of the list. Here, "90->61->67->100" is the list which represents the last 4 nodes.
When we move this list to the front then the remaining part of the initial list which is, "10->6->77" is attached after 100. Hence,
the new list formed with an updated head pointing to 90.
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def length(head):
count = 0
while head is not None:
head = head.next
count += 1
return count
def append_LinkedList(head, n):
if head == None or head.next == None:
return head
curr = head
temp2 = head
count = 0
l = length(head)
while count < l - n - 1:
curr = curr.next
count += 1
temp1 = curr.next
head = temp1
curr.next = None
while temp1.next != None:
temp1 = temp1.next
temp1.next = temp2
return head
def ll(arr):
if len(arr) == 0:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
def printll(head):
while head:
print(head.data, end=' ')
head = head.next
print()
arr = list(int(i) for i in input().strip().split(' '))
l = ll(arr[:-1])
i = int(input())
l = append_LinkedList(l, i)
printll(l)
#Eliminate duplicates from LL
"""
You have been given a singly linked list of integers where the elements are sorted in ascending order.
Write a function that removes the consecutive duplicate values such that the given list only contains unique elements and returns the head to the updated list.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run.
Then the test cases follow.
The first and the only line of each test case or query contains the elements(in ascending order) of the singly linked list separated by a single space.
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.
Output format :
For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space.
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
1
1 2 3 3 4 3 4 5 4 5 5 7 -1
Sample Output 1 :
1 2 3 4 3 4 5 4 5 7
Sample Input 2 :
2
10 20 30 40 50 -1
10 10 10 10 -1
Sample Output 2 :
10 20 30 40 50
10
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def eliminate_duplicate(head):
temp = head
if temp is None:
return
while temp.next is not None:
if temp.data == temp.next.data:
new = temp.next.next
temp.next = None
temp.next = new
else:
temp = temp.next
return head
def ll(arr):
if len(arr) == 0:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
def printll(head):
while head:
print(head.data, end=' ')
head = head.next
print()
arr = list(int(i) for i in input().strip().split(' '))
l = ll(arr[:-1])
l = eliminate_duplicate(l)
printll(l)
#Print Reverse LinkedList
"""
You have been given a singly linked list of integers. Write a function to print the list in a reverse order.
To explain it further, you need to start printing the data from the tail and move towards the head of the list,
printing the head data at the end.
Note :
You can’t change any of the pointers in the linked list, just print it in the reverse order.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first and the only line of each test case or query contains the elements of the singly linked list separated by a single space.
Remember/Constraints :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.
Output format :
For each test case, print the singly linked list of integers in a reverse fashion, in a row, separated by a single space.
Output for every test case will be printed in a seperate line.
"""
Sample Input 1 :
1
1 2 3 4 5 -1
Sample Output 1 :
5 4 3 2 1
Sample Input 2 :
2
1 2 3 -1
10 20 30 40 50 -1
Sample Output 2 :
3 2 1
50 40 30 20 10
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
def reverse(head):
def __init__(self):
head = None
prev = None
current =head
while(current is not None):
next = current.next
current.next = prev
prev = current
current = next
head = prev
return head
def ll(arr):
if len(arr)==0:
return None
head = Node(arr[0])
last = head
for data in arr[1:]:
last.next = Node(data)
last = last.next
return head
def printll(head):
while head:
print(head.data, end=' ')
head = head.next
print()
from sys import setrecursionlimit
setrecursionlimit(10000)
arr=list(int(i) for i in input().strip().split(' '))
l = ll(arr[:-1])
r=reverse(l)
printll(r)
#Palindrome LinkedList
"""
You have been given a head to a singly linked list of integers. Write a function check to whether the list given is a 'Palindrome' or not.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
First and the only line of each test case or query contains the the elements of the singly linked list separated by a single space.
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.
Output format :
For each test case, the only line of output that print 'true' if the list is Palindrome or 'false' otherwise.
"""
Sample Input 1 :
1
9 2 3 3 2 9 -1
Sample Output 1 :
true
Sample Input 2 :
2
0 2 3 2 5 -1
-1
Sample Output 2 :
false
true
Explanation for the Sample Input 2 :
For the first query, it is pretty intuitive that the the given list is not a palindrome, hence the output is 'false'.
For the second query, the list is empty. An empty list is always a palindrome , hence the output is 'true'.
Solution :
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
self.last_node = None
def append(self, data):
if self.last_node is None:
self.head = Node(data)
self.last_node = self.head
else:
self.last_node.next = Node(data)
self.last_node = self.last_node.next
def get_prev_node(self, ref_node):
current = self.head
while (current and current.next != ref_node):
current = current.next
return current
def is_palindrome(llist):
start = llist.head
end = llist.last_node
while (start != end and end.next != start):
if start.data != end.data:
return False
start = start.next
end = llist.get_prev_node(end)
return True
a_llist = LinkedList()
data_list = input().split()
for data in data_list:
if int(data) != -1:
a_llist.append(int(data))
if is_palindrome(a_llist):
print('true')
else:
print('false')