学无止境----编程练习

目录

1、反转链表:反转链表_牛客题霸_牛客网

2、判断链表是否有环:判断链表中是否有环_牛客题霸_牛客网

3、跳台阶 跳台阶_牛客题霸_牛客网

4、合并有序数组:合并两个有序的数组_牛客题霸_牛客网


1、反转链表:反转链表_牛客题霸_牛客网

struct ListNode {
    int val;
    struct ListNode *next;
};

struct ListNode* ReverseList(struct ListNode* pHead ) {
    if (pHead == NULL) {
        return NULL;
    }
    struct ListNode *tmp = pHead->next;
    struct ListNode *out = pHead;
    out->next = NULL;
    while (tmp) {
        pHead = tmp;
        tmp = pHead->next;
        pHead->next = out;
        out = pHead;
    }
    return out;
}

2、判断链表是否有环:判断链表中是否有环_牛客题霸_牛客网

bool hasCycle(struct ListNode *head) {
    struct ListNode *slow = head, *fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast) {
            return true;
        }
    }
    return false;
}

3、跳台阶 跳台阶_牛客题霸_牛客网

int jumpFloor(int number ) {
    // write code here
    int f0 = 1, f1 = 1, res = 0;
    if (number == 1)
        return 1;
    for (int i = 2; i <= number; ++i) {
        res = f0 + f1;
        f0 = f1;
        f1 = f2;
    }
    return f2;
}

4、合并有序数组:合并两个有序的数组_牛客题霸_牛客网

void merge(int* A, int ALen, int m, int* B, int BLen, int n) {
    int index = ALen + BLen - 1;
    int i = ALen - 1, j = BLen - 1;
    while (i >= 0 && j >= 0) {
        if (A[i] < B[j]) {
            A[index--] = B[j--];    
        } else {
            A[index--] = A[i--];
        }
    }
      
    while(j >= 0) {
        A[index--] = B[j--];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值