Lecture 1 : (Learn 1D Linked List)¶
Creating a linked list node¶
In [ ]:
class Node:
def __init__(self,val=0,next=None):
self.val = val
self.next = next
node = Node(100)
print(node.val)
print(node.next)
100 None <__main__.Node object at 0x103db5310>
Convert list to linked list¶
In [4]:
def to_linked_list(nums):
n =len(nums)
if n ==0:
return None
head = Node(nums[0])
temp = head
for i in range(1,n):
num = nums[i]
node = Node(num)
temp.next = node
temp = temp.next
return head
to_linked_list([1,2,3,4,5])
Out[4]:
<__main__.Node at 0x103ddff10>
More pythonic implementation¶
In [7]:
def to_linked_list(nums):
if not nums:
return None
head = Node(nums[0])
node = head
for num in nums[1:]:
node.next = Node(num)
node = node.next
return head
to_linked_list([1,2,3,4,5])
Out[7]:
<__main__.Node at 0x103db4050>
Print 1D linked list¶
In [11]:
def print_1d_linked_list(head):
if head == None:
print("Empty")
node = head
while node != None:
print(f"{node.val}",end=" ,")
node = node.next
print()
ll = to_linked_list([1,2,3,4,5])
print_1d_linked_list(ll)
1 ,2 ,3 ,4 ,5 ,
Insert at the head of a Linked List¶
Given a linked list and an integer value val, insert a new node with that value at the beginning (before the head) of the list and return the updated linked list.
Input Format: 0->1->2, val = 5
Result: 5->0->1->2
Input Format:12->5->8->7, val = 100
Result: 100->12->5->8->7
Approach 1¶
- create a new node with val
- return the node if empty list
- node.next = head and head = node
In [13]:
def insert_beg(ll,val):
node = Node(val)
if ll == None:
return node
node.next = ll
return node
print_1d_linked_list(insert_beg(to_linked_list([0,1,2]),5))
print_1d_linked_list(insert_beg(to_linked_list([12,5,8,7]),100))
5 ,0 ,1 ,2 , 100 ,12 ,5 ,8 ,7 ,
Approach 2 (Cleaner)¶
- use Node(val,NEXT)
In [14]:
def insert_beg(ll,val):
node = Node(val,ll)
return node
print_1d_linked_list(insert_beg(to_linked_list([0,1,2]),5))
print_1d_linked_list(insert_beg(to_linked_list([12,5,8,7]),100))
5 ,0 ,1 ,2 , 100 ,12 ,5 ,8 ,7 ,
Complexity¶
O(1)
O(1)
Length of linked list¶
Given the head of a linked list, print the length of the linked list.
Input Format: 0->1->2
Result =3
Input Format: 2->5->8->7
Result: 4
Approach 1 (Brute Force)¶
- linear traversal and keep track of count
In [ ]:
def linked_list_length(head):
count = 0
node = head
while node != None:
count +=1
node = node.next
return count
print(linked_list_length(to_linked_list([0,1,2])))
print(linked_list_length(to_linked_list([2,5,8,7])))
3 4
Complexity¶
O(N) : N = number of elements in the linked list
O(1)
Approach 2 (Using Recursion)¶
- length(None) = 0
- length(node) = 1 + length(node.next)
In [21]:
def linked_list_length(head):
if head is None:
return 0
return 1 + linked_list_length(head.next)
print(linked_list_length(to_linked_list([0,1,2])))
print(linked_list_length(to_linked_list([2,5,8,7])))
3 4
Complexity¶
O(N) : N = number of elements in LL
O(N) : N = number of elements in LL
Search an element in a Linked List¶
Given the head of a linked list and an integer value, find out whether the integer is present in the linked list or not. Return true if it is present, or else return false.
Input Format: 0->1->2, val = 2
Result True
Input Format: 12->5->8->7, val = 6
Result False
Approach 1 (Using Linear Search)¶
In [27]:
def check_val(head,val):
if head is None:
return False
node = head
while node != None:
if node.val == val:
return True
node = node.next
return False
print(check_val(to_linked_list([0,1,2]),2) )
print(check_val(to_linked_list([12,5,8,7]),6) )
True False
Approach 2 (Cleaner Pythonic Iteration)¶
In [29]:
def check_val(head,val):
node = head
while node is not None:
if node.val == val:
return True
node = node.next
return False
print(check_val(to_linked_list([0,1,2]),2) )
print(check_val(to_linked_list([12,5,8,7]),6) )
True False
Complexity¶
O(N) : N = number of elements in linked list
O(1)