C: Method to return the nth item from the end of a linked list. — int value_n_from_end(node_t *head, int n) { if (n < 1 || head == NULL) { printf("Cannot get nth item from end."); exit(EXIT_FAILURE); } node_t *current = head; node_t *match = head; for (int i = 0; i < n && current; ++i) { current = current->next; } if (i != n) { printf("List is too short to get nth item from end."); exit(EXIT_FAILURE); } while (current) { current = current->next; match = match->next; } return match->val; }
G
1.2K
Google Interview
This flashcard deck made by jwasham contains knowledge about google interview. For more details, please follow https://github.com/jwasham/google-interview-university