Analysis, this problem can be iterated twice to get the length, and then output. Of course, it is possible to iterate over the result by traversing through a pointer p1 in front of it. The length difference between p2 and P1 is N, so that after p1 traverses, P2 points to the NTH node from the bottom.
#include <stdio.h> #include <stdlib.h> typedef struct d{ int data; struct d *next; }node; node *Creat(int *a,int len) { node *head=(node *)malloc(sizeof(node)); head->data=NULL; node *ret=head; for(int i=0; i<len; i++) { node *s=(node *)malloc(sizeof(node)); s->data=a[i]; s->next=NULL; head->next=s; head=s; } return ret; } void Print(node *h) { for(h=h->next; h; h=h->next) { printf("%d->",h->data); } } int FindX(node *h,int num) { node *tmp=h; int i,ret; for(i=0; h; h=h->next,i++) { if(i>=num) { tmp=tmp->next; If (I <num) {ret=-1; if(I <num) {ret=-1; }else { ret=tmp->data; } return ret; } int main () {int a [] =,2,3,4,5,6,7,8,9,10,11,12,13,14,15 {1}; int len=sizeof(a)/sizeof(a[0]); node *h=Creat(a,len); Print(h); int n=5; int ret=FindX(h,n); printf("\nThe num is %d\n",ret); }Copy the code