看了评论区,改正后ac

#include<iostream>
using namespace std;
struct Node {
    int data;
    Node* next;
};
int main() {
    int n;
    while (cin >> n)
    {
        Node* head = new Node;//头结点
        Node* p=head;       
        for (int i = 0; i < n; i++)
        {
            int val = 0;
            Node* q = new Node;
            cin >>val;
            q->data = val;
            p->next = q;
            p = q;
        }
        //!!!!重要,最后一个next一定置为null
        p->next = NULL;
        int k = 0;
        cin >> k;
        p = head;
        Node* pp = head;
        if (k > 0)
        {
            while (k--)
            {
                p = p->next;
            }
            while (p->next != NULL) {
                p = p->next;
                pp = pp->next;
            }
            //这里由于从head开始,所以实际上是k+1个
            pp = pp->next;
            cout << pp->data << endl;
        }
        else if (k == 0)
        {
            cout << "0" << endl;
        }

    }
    return 0;
}