#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode* next;
};
// 创建链表
struct ListNode* createList(int len) {
struct ListNode* testlist = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode));
head = testlist;
for (int i = 1; i <= len; i++) {
testlist->val = i;
if (i == len) {
testlist->next = NULL;
break;
}
testlist->next = (struct ListNode*)malloc(sizeof(struct ListNode));
testlist = testlist->next;
}
return head;
}
struct ListNode* Find_k(struct ListNode* head, int k) {
struct ListNode* temp1 = head;
struct ListNode* temp2 = head;
int i = 0;
// 两个指针一前一后遍历,间隔k个节点
while (temp1->next != NULL) {
temp1 = temp1->next;
i++;
if (i >= k) {
temp2 = temp2->next;
}
}
printf("%d", temp2->val);
return 0;
}
int main() {
int a;
int len = 7;
struct ListNode* head;
scanf("%d", &a);
head = createList(len);
Find_k(head, a);
free(head);
}