ListNode FindnthToLast(int n) { if (n <1 || headNode == null) return null; ListNode pntr1 = headNode, pntr2 = headNode; //advance pntr2 by n-1 nodes; for (int i = 0; i < n - 1; ++i) { if (pntr2 == null) { return null; } else //go to the next node pntr2 = pntr2.getNext(); } while(pntr2.getNext() != null) { pntr1 = pntr1.getNext(); pntr2 = pntr2.getNext(); } return pntr1; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)