推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
NaVient
V2EX  ›  Python

请教一个关于链表的问题

  •  
  •   NaVient · Sep 6, 2018 · 3777 views
    This topic created in 2979 days ago, the information mentioned may be changed or developed.

    假如我现在有两个链表 A, B
    A 链表: 1 -> 3 -> 5
    B 链表: 2 -> 4 -> 6

    最终合并结果是 链表: 1 -> 2 -> 3 -> 4 -> 5 -> 6 该如何做?

    12 replies  •  2018-09-07 14:16:55 +08:00
    frandy
    frandy
    Sep 6, 2018
    1
    a = [1,3,5]
    b = [2,4,6]
    c = a+b
    c.sort()
    print(c)
    meik2333
    meik2333
    Sep 6, 2018
    2
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
    if (l1 == nullptr) {
    return l2;
    } else if (l2 == nullptr) {
    return l1;
    }
    auto *head = new ListNode(0);
    auto *cur = head;
    while (l1 and l2) {
    if (l1->val < l2->val) {
    cur->next = l1;
    l1 = l1->next;
    } else {
    cur->next = l2;
    l2 = l2->next;
    }
    cur = cur->next;
    }
    cur->next = l1 ? l1 : l2;
    cur = head->next;
    delete head;
    return cur;
    }
    cjw1115
    cjw1115
    Sep 6, 2018
    3
    @frandy 这操作还是很皮的
    dbw9580
    dbw9580
    Sep 6, 2018 via Android
    5
    list(itertools.chain.from_iterable(zip(a,b)))
    zyp0921
    zyp0921
    Sep 6, 2018
    6
    比大小呗- -
    anonymous256
    anonymous256
    Sep 6, 2018 via Android
    7
    c = [*a, *b]
    c.sort()
    print(c)
    seven2016
    seven2016
    Sep 6, 2018
    8
    链表常规题--归并

    ![归并]( https://zhimap.com/res/9/7/1532918487486844736.png)
    someonedeng
    someonedeng
    Sep 6, 2018❤️2
    9
    作业要自己做。
    stargazer
    stargazer
    Sep 6, 2018
    10
    合并有序链表,,,
    tt67wq
    tt67wq
    Sep 7, 2018
    11
    归并排序似乎就是这个
    Cukuyo
    Cukuyo
    Sep 7, 2018
    12
    作业要自己做
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5424 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 62ms · UTC 02:07 · PVG 10:07 · LAX 18:07 · JFK 21:07
    ♥ Do have faith in what you're doing.