Hide Index
- Understanding insertion at beginning
- Insert at beginning
- Understanding insertion at end
- Insert at end
- Understanding insertion after the given node
- Insert after the given node
- Understanding insertion before a given node
- Insert before the given node
- Understanding insertion at a given distance
- Insert at given distance
- Understanding deletion of first node
- Delete first node
- Understanding deletion of last node
- Delete last node
- Understanding deletion by given data
- Delete node with given data
- Delete nodes with given data
- Understanding deletion after a given node
- Delete node after the given node
- Understanding deletion before a given node
- Delete node before the given node
- Understanding deletion of the given node
- Delete the given node
- Understanding deletion at a given distance
- Delete node at given distance
Understanding the two pointer pattern
To perform any operation on the data items in a singly linked list, we must traverse it from head to tail and find those items. A doubly linked list, however, can be traversed in two directions, i.e., either from head to tail or tail to head, and depending on the problem, we may choose one direction over the other.
However, some problems require us to traverse the linked list in both directions simultaneously. While this is impossible with singly linked lists, we can simultaneously traverse in both directions in a doubly linked list using the two-pointer technique. The two-pointer traversal technique allows us to solve certain problems in linear time and single-pass, which would otherwise require inefficient nested loops.
The two-pointer pattern is a classification of problems that can be solved using the two-pointer traversal technique.
Liking the course? Check our discounted plans to continue learning.