Find nth last number in a singly linked list.
Anonymous
Yes, using two pointers is a good way: public static Node getNthLast(LinkedList ll, int n) { Node node1 = ll.getHead(); Node node2 = node1; int i = 0; while(node1 != null) { node1 = node1.getNext(); if(i >= n) { node2 = node2.getNext(); } i++; } return node2; }
Check out your Company Bowl for anonymous work chats.