输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针。
链表结点定义如下:
struct ListNode
{
int val;
ListNode* m_pNext;
}; 正常返回倒数第k个结点指针。
输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针。
struct ListNode
{
int val;
ListNode* m_pNext;
}; 每一个测试用例会有多组。每一组的测试用例格式如下:第一行输入链表结点个数,
第二行输入长度为的数组
,表示链表的每一项,
第三行输入的值,
![]()
每一组,输出倒数第k个结点的值
3 1 2 3 1 8 1 2 3 4 5 6 7 8 4
3 5
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int m_nKey;
struct ListNode *m_pNext;
};
void insertNode(struct ListNode *head, int value)
{
struct ListNode *next = head->m_pNext;
struct ListNode *prev = head;
for(; next != 0; next = next->m_pNext){
prev = next;
}
struct ListNode *temp = malloc(sizeof(struct ListNode));
temp->m_nKey = value;
temp->m_pNext = prev->m_pNext;
prev->m_pNext = temp;
return ;
}
int main() {
int num;
struct ListNode *head;
head = malloc(sizeof(struct ListNode));
head->m_nKey = -1;
head->m_pNext = 0;
while(scanf("%d", &num) != EOF){
int value;
for(int i=0;i<num;i++){
scanf("%d", &value);
insertNode(head, value);
}
int index = 0;
scanf("%d", &index);
//1 2 3 4 5 6 7 8
struct ListNode *front = head->m_pNext;
struct ListNode *back = head->m_pNext;
while(index--){
front = front->m_pNext;
}
while (front) {
front = front->m_pNext;
back = back->m_pNext;
}
printf("%d\n", back->m_nKey);
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
typedef struct linklist{
int value;
struct linklist *next;
}link;
int main()
{
int num, k, i;
int in[1000];
while(scanf("%d",&num) != EOF)
{
for(i=0;i<num;i++)
{
scanf("%d",&in[i]);
}
scanf("%d",&k);
link *head, *normal;
head=(link*)malloc(sizeof(link));
head->value=in[0];
link *p=head;
for(i=1;i<num;i++) //创建链表
{
normal=(link*)malloc(sizeof(link));
normal->value=in[i];
p->next=normal;
normal->next=NULL;
p=p->next;
}
link *fast=head; //快指针先走到第k个结点
i=1;
while(i<k)
{
fast=fast->next;
i++;
}
link *slow=head; //慢指针和快指针一起走,快指针走到最后一个结点跳出循环
while(fast->next != NULL)
{
fast=fast->next;
slow=slow->next;
}
printf("%d\n",slow->value);
}
return 0;
} #include <stdio.h>
typedef struct LinkList{
int data;
struct LinkList *next;
} *LinkList, LNode;
int main() {
int n;
while(scanf("%d", &n) != EOF)
{
LinkList L = (LinkList)malloc(sizeof(LinkList)); //初始化头结点
L->next = NULL;
for(int i=0; i<n; i++){
LNode* node = (LNode*)malloc(sizeof(LNode));
scanf("%d", &node->data);
node->next = NULL;
LNode* p = L;
while(p->next)
{
p = p->next; //找到最后一个结点
}
p->next = node; //尾插法
}
int k;
scanf("%d", &k);
LNode* fast = L;
LNode* low = L;
int step = 0;
while(step < k) //快指针先走k步
{
fast = fast->next;
step++;
}
while(fast) //快、慢指针一起走,直到快指针为空,此时慢指针就是倒数第k个结点
{
low = low->next;
fast = fast->next;
}
printf("%d\n", low->data);
}
return 0;
} #include <stdio.h>
struct ListNode
{
int m_nKey;
struct ListNode* m_pNext;
};
int main(){
struct ListNode *head = NULL, *tmp, *p;
int num;
int m_nKey;
int i = 0;
int k;
while(scanf("%d", &num) != EOF){
for(i = 0; i < num; i++){
p = (struct ListNode *)malloc(sizeof(struct ListNode));
scanf("%d", &m_nKey);
p->m_nKey = m_nKey;
p->m_pNext = NULL;
if(head == NULL)
head = p;
else
tmp->m_pNext = p;
tmp = p;
}
tmp = head;
scanf("%d", &k);
for(i = 0; i < num-k;i++){
tmp = tmp->m_pNext;
}
printf("%d", tmp->m_nKey);
while(head != NULL){
tmp = head;
head = tmp->m_pNext;
free(tmp);
}
printf("\n");
}
return 0;
}
#include<stdio.h>
struct ListNode
{
int m_nKey;
struct ListNode* m_pNext;
};
struct ListNode* listinit(int headval){
struct ListNode* head = (struct ListNode*)malloc(sizeof(struct ListNode)) ;
head->m_nKey = headval ;
head->m_pNext = NULL ;
return head ;
}
void listadd(int val,struct ListNode*head){
struct ListNode* node = (struct ListNode*)malloc(sizeof(struct ListNode)) ;
node->m_nKey = val ;
node->m_pNext = NULL ;
head->m_pNext = node ;
return ;
}
int main(){
int n ;
int val ;
while(scanf("%d", &n) != EOF){
scanf("%d",&val) ;
struct ListNode* head = listinit(val) ;
struct ListNode* k = head ;
for(int i = 0;i<n-1 ; i++){
scanf("%d",&val) ;
listadd(val, k) ;
k = k->m_pNext ;
}
int kk ;
scanf("%d",&kk) ;
struct ListNode* quetnode = head ;
struct ListNode* slownode = head ;
for(int i = 0;i<kk-1 ; i++){
if(!quetnode) return 0 ;
quetnode = quetnode->m_pNext ;
}
while(quetnode->m_pNext){
quetnode = quetnode->m_pNext ;
slownode = slownode->m_pNext ;
}
printf("%d\n",slownode->m_nKey) ;
}
return 0 ;
} #include<stdio.h>
//定义一个单链表
typedef struct Listnode
{
int data;
struct Listnode* next;
}Listnode;
int returnback(Listnode*head,int n,int k)
{
Listnode* tail=head;
if(k>n||k==0)
return NULL;
for(int i=0;i<k-1;i++)
{
tail=tail->next;
}
return tail->data;
}
int main()
{
int n=0,k=0;
while(scanf("%d",&n)!=EOF)
{
Listnode* head=NULL;
for(int i=0;i<n;i++)
{
Listnode* newnode=(Listnode*)malloc(sizeof(Listnode));
scanf("%d",&newnode->data);
newnode->next=head;
head=newnode;
}
scanf("%d",&k);
printf("%d\n",returnback(head, n, k));
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
int main()
{
int N;
int k;
int i;
int arr[1001] ={0};
while(EOF!=scanf("%d",&N))
{
for(i=0;i<N;i++)
{
scanf("%d",&arr[i]);
}
scanf("%d",&k);
printf("%d\n",(k<=0||k>N)?0:arr[N-k]);
}
return 0;
} #include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ListNode
{
int Key;
struct ListNode *next;
}ListNode, *List;
int main()
{
int len = 0;
int pos = 0;
List L1 = NULL;
ListNode *p = NULL;
while(scanf("%d", &len) != EOF)
{
L1 = (ListNode*)malloc(sizeof(ListNode));
L1->next = NULL;
for(int i=0; i < len; i++)
{
p = (ListNode*)malloc(sizeof(ListNode));
scanf("%d", &(p->Key));
p->next = L1->next;
L1->next = p;
}
scanf("%d", &pos);
p = L1;
for(int i=1; i<= pos; i++)
{
p = p->next;
}
printf("%d\n", p->Key);
/*释放内存--删除操作*/
for(int i=0; i<len; i++)
{
p = L1->next;
L1->next = p->next;
free(p);
}
free(L1);
}
}