#include<iostream> #include<algorithm> using namespace std; const int N = 1005; int head, e[N], ne[N], idx; void init () { idx = 0, head = -1; } void add_to_tail(int x) { e[idx] = x; ne[idx] = -1; if (head == -1) head = idx++; else { int t = head; while (ne[t] != -1) t = ne[t]; ne[t] = idx++; } } int main() { int n, k; while (cin >> n) { init(); while (n--) { int x; cin >> x; add_to_tail(x); } cin >> k; int i = head, j = i; while (k--) j = ne[j]; while (ne[j] != -1) { i = ne[i], j = ne[j]; } cout << e[i + 1] << endl; } return 0; }
双指针算法,数组模拟链表。