Previous Article: Finding first node in a Loop in Singly Linked List.
Figure 1: Singly Linked List |
Solution 01 - Brute Force Approach:
- Start at First Node of the List (call it curNodePtr).
- Assign curNodePtr to tmpPtr and count number of nodes after the curNodePtr.
- If number of nodes after curNodePtr are equal to N nodes or tmpPtr reaches END then break. If tmPtr reaches END but count not equal to N then return since we can't find the Nth node from the end of the Singly Linked List.
- Move the curNodePtr one step forward in the Linked List i.e curNodePtr now points to its next node in the list and start again from STEP-2.