-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedLists.py
76 lines (56 loc) · 1.73 KB
/
linkedLists.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
# DONT CHANGE THIS CODE
# (UNLESS YOU NEED TO DO SPECIFIC MODIFICATIONS TO THE FUNCTIONS)
# Insert a node in a given Singly Linked List (recursively)
class Node:
# Structure of the node.
def __init__(self, val):
self.val = val
self.next = None
# Insert a node at first position in the given Linked List
def insertAtFirst(listHead, val):
new = Node(val)
new.next = listHead
return new
# Time Complexity - O(1)
# Insert a node at the end of the given Linked List (recursively)
def insertAtEnd(listHead, val):
if listHead is None:
new = Node(val)
return new
if listHead.next is None:
new = Node(val)
listHead.next = new
return listHead
insertAtEnd(listHead.next, val)
return listHead
# Time Complexity - O(n)
# Insert a node at a specific position in the given Linked List (recursively)
def insertInBetween(temp, val, pos):
# if head is None and given position is greater than 0
if temp is None and pos>0:
return temp
# if the node is to be added at first position
if pos == 0:
new = Node(val)
new.next = temp
return new
# if the node is to be added at second position
if pos == 1:
new = Node(val)
new.ext = temp.next
temp.next = new
return temp
insertInBetween(temp.next, val, pos-1)
return temp
# Time Complexity - O(i)
# Function to print the Linked List through Recursion
def printll(head):
if head == None:
return
print(head.val, end=' -> ')
printll(head.next)
# Time Complexity - O(n)
# where n is the length of the linked list
if __name__ == '__main__':
pass
'''Implemented and adapted by Alejandro Ríos (GitHub: @alejoriosm04)'''