We will discuss basic operations on singly linked list. Operations like insert,delete and print.

**1. Insert:**

In singly linked list, last node stores next address value as null. So while inserting new node , linked list is traversed from start to end.

New node will be inserted at the end so last node’s next address will be pointed to new node.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public void Insert(Node head, int val) { if (head == null) { head = new Node(val, null); return; } Node tnode; for (tnode = head; tnode.next != null; tnode = tnode.next) { } tnode.next = new Node(val, null); return; } |

As all nodes are visited while inserting new value, time complexity for this operation is O(n).

**2. Insert at specific position:**

Given a value and position where you want to insert. First traverse through linked list until we get node previous to desired position and then insert node at that position. In this problem we also need to check if given position is out of range of given linked list. For example, currently linked list contains only 4 nodes and for new value position is given as 10, which is incorrect.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
Node InsertPosition(Node head, int position, int val) { if (position == 0) { Node tnode = new Node(val, null); tnode.next = head; head = tnode; return head; } Node tempnode = head; for (int i = 0; i < position - 1; i++) { if (tempnode == null || (i < position - 1 && tempnode.next == null)) { Console.WriteLine("Entered position " + position + " is incorrect"); return head; } tempnode = tempnode.next; } Node newNode = new Node(val, tempnode.next); tempnode.next = newNode; return head; } |

In this case best case is insert value at start, for this time complexity will be O(1) and worst case is insert at last position or position is out of range, in this scenario all nodes of linked list will be traversed so time complexity will be O(n).

**3. Delete node from at specific position:**

Given position at which you want to delete a node . First traverse through linked list until we get node previous to desired position and then delete node at that position. In this problem we also need to check if given position is out of range of given linked list. For example, currently linked list contains only 4 nodes and position is given as 10, which is incorrect.

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 |
public Node Delete(Node head, int position) { if (position == 0) { head = head.next; return head; } Node prev = head; Node nextNode = head.next; for (int i = 1; i < position - 1; i++) { prev = nextNode; nextNode = nextNode.next; if (nextNode.next == null && i == position - 1) { prev.next = null; return head; } if (prev.next == null && i < position - 1) { Console.WriteLine("Node does not exist at given postion : " + position); Console.WriteLine("Elements present in linked list are :"); return head; } } prev.next = nextNode.next; return head; } |

In this case best case is delete node at position 0 which is head of linked list , for this time complexity will be O(1) and worst case is delete last node or position is out of range, in this scenario all nodes of linked list will be traversed so time complexity will be O(n).

**4. Print all linked list values:**

This is a simple operation on a linked list.

1 2 3 4 5 6 7 8 9 |
public void Print(Node node) { if (node != null) { Console.Write(" " + node.val); Print(node.next); } return; } |

As all nodes are visited to print values, time complexity of this operation is O(n).

You can find solution to more problem on linked list here.