博客
关于我
学会链表中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/

你可能感兴趣的文章
pintos project (2) Project 1 Thread -Mission 1 Code
查看>>
PinYin4j库的使用
查看>>
PIP
查看>>
pip install goose-extractor // SyntaxError: Missing parentheses in call to 'print'
查看>>
pip install mysqlclient报错
查看>>
pip install 出现报asciii码错误的解决
查看>>
pip throws TypeError: parse() got an unexpected keyword argument ‘transport_encoding‘ 在尝试安装新软件包时
查看>>
pip 下载慢
查看>>
pip 升级报错AttributeError: ‘NoneType’ object has no attribute ‘bytes’
查看>>
pip 安装opencv-python卡死
查看>>
pip 安装出现异常
查看>>
Pip 安装失败:需要 SSL
查看>>
Pip 安装挂起
查看>>
pip 或 pip3 为 Python 3 安装包?
查看>>
pip 文件损坏导致 pip无法使用 报错 ImportError: cannot import name 'main' from 'pip._int
查看>>
pip 无法从 requirements.txt 安装软件包
查看>>
pip/pip3更换国内源
查看>>
pip3 install PyQt5 --user 失败
查看>>
pip3命令全解析:Python3包管理工具的详细使用指南
查看>>
pip3安装命令重复创建文件‘/tmp/pip-install-xxxxx/package‘失败
查看>>