往年EMC编程题答案

本文介绍了如何通过双指针法找到单链表的倒数第K个节点,并阐述了该算法的时间和空间复杂度。此外,还详细解释了一个用于在整数数组中查找第二大数的函数,包括其核心逻辑和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 Write a function to find the Kth node form the last node of a singly-linked list, and analze the time and space complexity of your funciton.
The input to the function is a pointer or reference to the head of the list.
The output is a pointer or reference to the Kth node from the last node.

 

用两个指针p,   q同时指向第一个节点,   q先向前走n步,   然后p,   q同时前进,   当q走到最后一个节点时,   p即指向倒数第n个.


typedef   struct
{
        int   i;
        Node   *next;
}Node;

Node   *test(Node   *t,int   n)
{

        Node   *p,   *q;

        p   =   t;
        q   =   t;

        while(n--   >   0)   //没有考虑接点个数小于n的情况  
                q   =   q-> next;

        while(q   !=   NULL)
        {
                p   =   p-> next;
                q   =   q-> next;
        }

        return   p;
}

 

2

写一个函数找出一个整数数组中,第二大的数
#include <stdio.h>

const int MINNUMBER = -32767

int find_sec_max(int data[], int count)
{
     int maxnumber = data[0];
     int sec_max = MINNUMBER;
     int i = 0;
     
      for(i = 1; i < count; i++){
             if(data[i] > maxnuber){
                    sec_max = maxnumber;
                    maxnumber = data[i]:
             }
             else if(data[i] > sec_max){
                     sec_max = data[i];
              }
        }
        return sec_max;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值