#include
struct ListNode {
int val;
struct ListNode* next;
};
int main()
{
struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
n1->val = 1;
n2->val = 2;
n3->val = 3;
n4->val = 4;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = NULL;
struct ListNode* fast, * slow;
fast = slow = n1;
while (fast)
{
fast = fast->next->next;
slow = slow->next;
}
while (slow)
{
printf("%d", slow->val);
slow = slow->next;
}
return 0;
}
这段代码没发现问题,是否是 malloc()函数的头文件缺了,应该加上 #include <stdlib.h>