Implement an algorithm to return the nth element (keep it up) from a single linked list)
We maintain two pointers with a distance of n. Then, I move these two pointers synchronously on the single-chain table to keep them at a distance of n. So,
When the second pointer is null, the first pointer is required.
# Include
Struct Node {int data; Node * next;}; void initList (Node * vNode) {for (int I = 0; I <20; ++ I) {Node * TempNode = new Node; TempNode-> data = I; TempNode-> next = vNode-> next; vNode-> next = TempNode ;}} node * getNthBackWards (const Node * vNode, int vN) {if (vN <1 | vNode = NULL) return NULL; Node * p = vNode; Node * q = vNode; while (vN> 0) {++ q; if (q = NULL) return NULL; -- vN;} while (q! = NULL) {++ q; ++ p;} return p;} int main () {Node * Root = new Node; Root-> next = NULL; initList (Root); Node * Result = getNthBackWards (Root, 7); std: cout <Result-> data <std: end; return 0 ;}