HJ51 输出单向链表中倒数第k个结点

描述

输入一个单向链表,输出该链表中倒数第k个结点,链表的倒数第1个结点为链表的尾指针。

链表结点定义如下:
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
正常返回倒数第k个结点指针,异常返回空指针.
要求:
(1)正序构建链表;
(2)构建后要忘记链表长度。
数据范围:链表长度满足 1≤n≤1000,k≤n,链表中数据满足 0≤val≤10000

本题有多组样例输入。

输入描述
输入说明
1 输入链表结点个数
2 输入链表的值
3 输入k的值

输出描述
输出一个整数

示例1

输入:
8
1 2 3 4 5 6 7 8 4
输出:
5

示例2

输入
1
8108
1
7
2542 4218 9064 4908 1526 6655 921
1
输出
8108
921

实现

#include 
#include 
#include 

struct ListNode
{
    int m_nKey;
    struct ListNode *m_pNext;
} ListNode;

struct ListNode *createNode(int val)
{
    struct ListNode *node = NULL;
    node = (struct ListNode *)malloc(sizeof(struct ListNode));
    if (node == NULL) {
        return node;
    }

    memset(node, 0, sizeof(struct ListNode));
    node->m_nKey = val;
    return node;
}

struct ListNode *initListHead()
{
    struct ListNode *head = NULL;
    head = (struct ListNode *)malloc(sizeof(struct ListNode));
    if (head == NULL) {
        return head;
    }

    memset(head, 0, sizeof(struct ListNode));
    return head;
}
void addListNode(struct ListNode *head, struct ListNode *node)
{
    struct ListNode *tmpNode = head;
    while (tmpNode->m_pNext != NULL) {
        tmpNode = tmpNode->m_pNext;
    }

    tmpNode->m_pNext = node;
}

struct ListNode *getListNode(struct ListNode *head, int index) 
{
    struct ListNode *node = head;
    // printf("index: %d\n", index);
    while (index > 0) {
        index--;
        node = node->m_pNext;
    }
    return node;
}

void displayList(struct ListNode *head)
{
    int index = 0;
    struct ListNode *tmpNode = head->m_pNext;
    printf("Display:\n");
    while (tmpNode != NULL) {
        printf("\tindex: %d, %p => [%d-%p]\n", index++, tmpNode, tmpNode->m_nKey, tmpNode->m_pNext);
        tmpNode = tmpNode->m_pNext;
    }
}

void freeList(struct ListNode *head)
{
    struct ListNode *tmpNode = NULL;
    while (head->m_pNext != NULL) {
        tmpNode = head->m_pNext;
        head->m_pNext = tmpNode->m_pNext;
        free(tmpNode);
    }
}
int main() {
    int ret;
    int listNum, val, index;
    struct ListNode *node = NULL;
    struct ListNode *head = NULL;

    while((ret = scanf("%d", &listNum)) != EOF) {
        // printf("listNum: %d, ret: %d\n", listNum, ret);
        head = initListHead();
        if (head == NULL) {
            printf("initListHead fail\n");
            return -1;
        }
        // printf("listNum: %d\n", listNum);
        for (int i = 0; i < listNum; i++) {
            scanf("%d", &val);
            node = createNode(val);
            if (node == NULL) {
                printf("createNode fail\n");
                return -1;
            }
            // printf("[%d-%p] %p\n", node->m_nKey, node->m_pNext, node);
            addListNode(head, node);
        }

        scanf("%d\n", &index);
        // printf("index: %d\n", index);
        // displayList(head);
        node = getListNode(head, listNum - index + 1);
        // printf("node: %p\n", node);
        printf("%d\n", node->m_nKey);
        freeList(head);
        free(head);
        head = NULL;
    }

    return 0;
}

你可能感兴趣的:(牛客网,-,练习,链表,数据结构,c++)