## DEV Community is a community of 667,956 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

CodeWithML
Sharing solutions to coding problems.
Originally published at codewithml.netlify.app ・4 min read

Problem statement: Implement a Linked List with it's functions like inserting an element at start of the linked list and inserting at the end of linked list, searching an element in the list and removing an element from the list.

It's a linear data structure, but the elements are not stored in next to each other or in a sequence, but rather they're linked using pointers.

Test Cases:

1. Insert at start

• Insert a None.
• Insert at start in an empty linked list.
• Insert at start in a non-empty linked list.
2. Insert at end.

• Insert a None.
• Insert at end in an empty linked list.
• Insert at end in a non-empty linked list.
3. Search

• Search for element present in linked list.
• Search for element in an empty linked list.
• Search for element not present in linked list.
• Search for None.
4. Remove

• Remove element from an empty linked list.
• Remove element from a non-empty linked list.
• Remove element not present in linked list.
• Remove a None.
5. Length

• Print length of the linked list.

Algorithm:

1. Insert to start
• If data is None
• return
• Create a new node with data
• Set the next of new node to head
• Set the head to the new node
2. Insert to end
• If data is None
• return
• Create a new node with data
• If linked list is empty,
• set the head to the node
• Else,
• iterate through the end of list
• Set the next of last node to new node.
3. Search

• Initialise a current pointer to head
• Iterate through the linked list
• If value is matched, return the value
• Else, move onto the next node
4. Remove

• Initialise a current pointer, pointing to node next to head
• Initialise a previous pointer, pointing to head
• Iterate through the linked list,
• If value is found,
• Set the previous node, next pointer to current node next
• Else, move onto the next node
5. Length

• Initialise a counter
• Iterate through the entire linked list
• For each node in the list,
• increase the counter
• Return the counter

Time Complexity:

1. Insert to start
• Time complexity: O(1)
• Space complexity: O(1)
2. Insert to end
• Time complexity: O(n)
• Space complexity: O(1)
3. Search
• Time complexity: O(n)
• Space complexity: O(1)
4. Remove
• Time complexity: O(n)
• Space complexity: O(1)
5. Length
• Time complexity: O(n)
• Space complexity: O(1)

Code

class Node(object):
def __init__(self, data, next=None):
self.data = data
self.next = next

def __len__(self):
counter = 0
while current is not None:
counter += 1
current = current.next
return counter

def insertStart(self, data):
if data is None:
return None
return node

def insertEnd(self, data):
if data is None:
return None
node = Node(data)
return node
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node

def search(self, data):
if data is None:
return None
while curr_node is not None:
if curr_node.data == data:
return curr_node.data
curr_node = curr_node.next
return None

def remove(self, data):
if data is None:
return None
return
return
while curr_node is not None:
if curr_node.data == data:
prev_node.next = curr_node.next
return
prev_node = curr_node
curr_node = curr_node.next

def remove2(self, data):
if data is None:
return None
return None
if curr_node.data == data:
curr_node = curr_node.next
return
while curr_node.next is not None:
if curr_node.next.data == data:
curr_node.next = curr_node.next.next
return
curr_node = curr_node.next

def printList(self):
while curr_node is not None:
print(curr_node.data)
curr_node = curr_node.next

def getData(self):
data = []
while curr_node is not None:
data.append(curr_node.data)
curr_node = curr_node.next
return data

Unit Test

import unittest

def testInsertStart(self):
print('Test: insertStart on an empty linked list')

print('Test: insertStart on a None')

print('Test: insertStart on a non-empty linked list')

print('Success: testInsertStart')

def testInsertEnd(self):
print('Test: insertLast on an empty linked list')

print('Test: insertEnd a None')

print('Test: insertEnd on a non-empty linked list')

print('Success: insertEnd')

def testSearch(self):
print('Test: Search on an empty linked list')
self.assertEqual(element, None)

print('Test: Search on a None')
self.assertEqual(element, None)

print('Test: Search on a non-empty linked list')
self.assertEqual(element, 11)
self.assertEqual(element2, None)
print('Success: testSearch')

def testRemove(self):
print('Test: remove element from an empty linked list')

print('Test: remove a None')

print('Test: remove element from a non-empty linked list')

print('Success: testRemove')

def testLen(self):
print('Test length of thean empty linked list')

print('Test length of non empty linked list')

print('Success: testLen')

def main():
test.testInsertStart()
test.testInsertEnd()
test.testSearch()
test.testRemove()
test.testLen()

if __name__ == "__main__":
main()

Github solution repo

Happy coding ⭐