面试题25:合并两个排序的链表
生活随笔
收集整理的這篇文章主要介紹了
面试题25:合并两个排序的链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
NowCoder
<?php header("content-type:text/html;charset=utf-8"); /** 輸入兩個單調遞增的鏈表,輸出兩個鏈表合成后的鏈表,當然我們需要合成后的鏈表滿足單調不減規則。 P145*/ class ListNode{var $val;var $next = NULL;function __construct($x){$this->val = $x;} } function Merge($pHead1, $pHead2) {if($pHead1 == null){return $pHead2;}if($pHead2 == null){return $pHead1;}// $pHead = new ListNode(null);if($pHead1->val <= $pHead2->val){$pHead = $pHead1;$pHead->next = Merge($pHead1->next,$pHead2);}else{$pHead = $pHead2;$pHead->next = Merge($pHead1,$pHead2->next);}return $pHead; }$head1 = new ListNode(1); $head1->next = new ListNode(4); $head1->next->next = new ListNode(5); $head1->next->next->next = new ListNode(7); $head1->next->next->next->next = new ListNode(8); $head1->next->next->next->next->next = new ListNode(12);$head2 = new ListNode(1); $head2->next = new ListNode(3); $head2->next->next = new ListNode(6); $head2->next->next->next = new ListNode(9); $head2->next->next->next->next = new ListNode(11);print_r(Merge($head1,$head2));?
轉載于:https://www.cnblogs.com/xlzfdddd/p/10198329.html
總結
以上是生活随笔為你收集整理的面试题25:合并两个排序的链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微信APP支付 C#
- 下一篇: The Things Network L