推荐学习书目
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 · 3408 views
    This topic created in 2832 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
        1
    frandy  
       Sep 6, 2018
    a = [1,3,5]
    b = [2,4,6]
    c = a+b
    c.sort()
    print(c)
    meik2333
        2
    meik2333  
       Sep 6, 2018
    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
        3
    cjw1115  
       Sep 6, 2018
    @frandy 这操作还是很皮的
    ihainan
        4
    ihainan  
       Sep 6, 2018 via iPhone
    dbw9580
        5
    dbw9580  
       Sep 6, 2018 via Android
    list(itertools.chain.from_iterable(zip(a,b)))
    zyp0921
        6
    zyp0921  
       Sep 6, 2018
    比大小呗- -
    anonymous256
        7
    anonymous256  
       Sep 6, 2018 via Android
    c = [*a, *b]
    c.sort()
    print(c)
    seven2016
        8
    seven2016  
       Sep 6, 2018
    链表常规题--归并

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