How would you get the nth node from the tail in a singly linked list (in one traverse)?
问题 So I got this question from an exam. How would you get the nth node from the tail in a singly linked list? Each Node has a value and a next (which is a pointer to the next value). We are given this: getNodeFromTail(Node head, int x) { } So the way I did it is to find the length of the list by traversing it once. Then going again to get the (length - x) node. So in total, 2 traversals. getNodeFromTail(Node head, int x) { int length = 0; Node headdupe = head; while (headdupe.next != NULL) {