寻找链表中倒数第k个数
#include <stdio.h> #include <stdlib.h> typedef struct Node{ int content; struct Node* next; }LNode; init(LNode** head){ *head = (LNode*)malloc(sizeof(LNode)); (*head)->content = 0; (*head)->next = NULL; } insert(LNode* head, int num){ LNode* newNode = (LNode*)malloc(sizeof(LNode)); newNode -> content = num; newNode -> next = head -> next; head -> next = newNode; } printL(LNode* head){ head = head -> next; while(head != NULL){ printf("%d ", head -> content); head = head -> next; } } find(LNode* head, int n){ int i = 0; LNode* temp = head; temp = temp -> next; head = head-> next; while(i < n){ temp = temp -> next; i++; } while(temp != NULL){ temp = temp -> next; head = head-> next; } printf("%d", head -> content); } main(){ LNode* head; init(&head); insert(head, 1); insert(head, 2); insert(head, 3); insert(head, 4); insert(head, 5); insert(head, 6); insert(head, 7); insert(head, 8); insert(head, 9); printL(head); find(head, 3); }