博客
关于我
学会链表中LeetCode三道题
阅读量:117 次
发布时间:2019-02-26

本文共 2560 字,大约阅读时间需要 8 分钟。

??????????

?????????

1. ????

????? head ?????????????????????????????????????????????????????????????

2. ????

??????????????????????????????????????????????????????????????????????

????

struct ListNode {    int val;    struct ListNode* next;};struct ListNode* middleNode(struct ListNode* head) {    struct ListNode* fast = head, *slow = head;    while (fast && fast->next) {        slow = slow->next;        fast = fast->next->next;    }    return slow;}

????????k???

1. ????

???????????????k????????????????1????

2. ????

??????????????????k??????????????????????????????????k????

????

struct ListNode {    int val;    struct ListNode* next;};struct ListNode* getKthFromEnd(struct ListNode* head, int k) {    struct ListNode* fast = head, *slow = head;    while (k--) {        if (fast == NULL) {            return NULL;        }        fast = fast->next;    }    while (fast) {        fast = fast->next;        slow = slow->next;    }    return slow;}

???????????

1. ????

??????????????????????????????

2. ????

???????????????????????????????????????????????????????????????????????????????

???????????

struct ListNode {    int val;    struct ListNode* next;};struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {    struct ListNode* head = NULL, *tail = NULL;    if (l1 == NULL) {        return l2;    }    if (l2 == NULL) {        return l1;    }    if (l1->val < l2->val) {        head = tail = l1;        l1 = l1->next;    } else {        head = tail = l2;        l2 = l2->next;    }    while (l1 && l2) {        if (l1->val < l2->val) {            tail->next = l1;            l1 = l1->next;        } else {            tail->next = l2;            l2 = l2->next;        }        tail = tail->next;    }    if (l1) {        tail->next = l1;    }    if (l2) {        tail->next = l2;    }    return head;}

??????????

struct ListNode {    int val;    struct ListNode* next;};struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {    struct ListNode* head = NULL, *tail = NULL;    head = tail = (struct ListNode*)malloc(sizeof(struct ListNode));    tail->next = NULL;    while (l1 && l2) {        if (l1->val < l2->val) {            tail->next = l1;            l1 = l1->next;        } else {            tail->next = l2;            l2 = l2->next;        }        tail = tail->next;    }    if (l1) {        tail->next = l1;    }    if (l2) {        tail->next = l2;    }    struct ListNode* node = head;    head = head->next;    free(node);    return head;}

??????????????????????????????????????????????????????????????

转载地址:http://gxrk.baihongyu.com/

你可能感兴趣的文章
ORA-12514: TNS:listener does not currently know of service问题原因
查看>>
ora-12541:tns:no listener
查看>>
【docker知识】联合文件系统(unionFS)原理
查看>>
ORACEL学习--理解over()函数
查看>>
ORAchk-数据库健康检查
查看>>
oracle 10g crs命令,Oracle 10g CRS安装问题解决一例
查看>>
Oracle 10g ORA-01034: ORACLE not available 错误
查看>>
oracle 10g的安装配置
查看>>
Oracle 11.2.0.4 x64 RAC修改public/private/vip/scan地址
查看>>
Oracle 11G INDEX FULL SCAN 和 INDEX FAST FULL SCAN 对比分析
查看>>
viewpage listview gridview加载本地大图多图OOM处理办法
查看>>
Oracle 11g UNDO表空间备份增强
查看>>
Oracle 11g 使用RMAN备份数据库
查看>>
Oracle 11g 单实例安装文档
查看>>
Oracle 11g 操作ASM权限问题
查看>>
Oracle 11g 数据类型
查看>>
Oracle 11g 编译使用BBED
查看>>
oracle 11g 静默安装
查看>>
Oracle 11gR2学习之二(创建数据库及OEM管理篇)
查看>>
Oracle 11gR2构建RAC之(2)--配置共享存储
查看>>