Example :
3 --------> 4 -------------> 5 ------------> 6
prev cur next
Suppose we want to delete element '5' here .
1. We need to traverse the linked list first until we reach prev node .
2. So once we reach prev node then we need to point prev reference to next .
Prev.next = cur.next;
After that :
3 -----------> 4 -------------> 6
That's how we delete an element.
Anyway it takes O(n) to traverse entire list and space complexity O(1).
Delete the First Node
1. In general entire LinkedList is represented with head node.
2. Just point head to head.next
This is how you will delete the head node.
No comments:
Post a Comment