@@ -295,24 +295,19 @@ var mergeTwoLists = function (list1, list2) {
295295 */
296296public class Solution {
297297 public ListNode MergeTwoLists (ListNode list1 , ListNode list2 ) {
298- ListNode dummy = new ListNode ();
299- ListNode cur = dummy ;
300- while (list1 != null && list2 != null )
301- {
302- if (list1 .val <= list2 .val )
303- {
304- cur .next = list1 ;
305- list1 = list1 .next ;
306- }
307- else
308- {
309- cur .next = list2 ;
310- list2 = list2 .next ;
311- }
312- cur = cur .next ;
298+ if (list1 == null ) {
299+ return list2 ;
300+ }
301+ if (list2 == null ) {
302+ return list1 ;
303+ }
304+ if (list1 .val <= list2 .val ) {
305+ list1 .next = MergeTwoLists (list1 .next , list2 );
306+ return list1 ;
307+ } else {
308+ list2 .next = MergeTwoLists (list1 , list2 .next );
309+ return list2 ;
313310 }
314- cur .next = list1 == null ? list2 : list1 ;
315- return dummy .next ;
316311 }
317312}
318313```
@@ -332,23 +327,61 @@ public class Solution {
332327# @ param {ListNode} list2
333328# @ return {ListNode}
334329def merge_two_lists (list1 , list2 )
335- dummy = ListNode .new ()
336- cur = dummy
337- while list1 && list2
338- if list1.val <= list2.val
339- cur.next = list1
340- list1 = list1.next
341- else
342- cur.next = list2
343- list2 = list2.next
344- end
345- cur = cur.next
330+ if list1.nil?
331+ return list2
332+ end
333+ if list2.nil?
334+ return list1
335+ end
336+ if list1.val <= list2.val
337+ list1.next = merge_two_lists(list1.next, list2)
338+ return list1
339+ else
340+ list2.next = merge_two_lists(list1, list2.next)
341+ return list2
346342 end
347- cur.next = list1 || list2
348- dummy.next
349343end
350344```
351345
346+ #### PHP
347+
348+ ``` php
349+ /**
350+ * Definition for a singly-linked list.
351+ * class ListNode {
352+ * public $val = 0;
353+ * public $next = null;
354+ * function __construct($val = 0, $next = null) {
355+ * $this->val = $val;
356+ * $this->next = $next;
357+ * }
358+ * }
359+ */
360+ class Solution {
361+
362+ /**
363+ * @param ListNode $list1
364+ * @param ListNode $list2
365+ * @return ListNode
366+ */
367+ function mergeTwoLists($list1, $list2) {
368+ if (is_null($list1)) {
369+ return $list2;
370+ }
371+ if (is_null($list2)) {
372+ return $list1;
373+ }
374+ if ($list1->val <= $list2->val) {
375+ $list1->next = $this->mergeTwoLists($list1->next, $list2);
376+ return $list1;
377+ } else {
378+ $list2->next = $this->mergeTwoLists($list1, $list2->next);
379+ return $list2;
380+ }
381+ }
382+ }
383+ ```
384+
352385<!-- tabs: end -->
353386
354387<!-- solution: end -->
@@ -603,6 +636,72 @@ var mergeTwoLists = function (list1, list2) {
603636};
604637```
605638
639+ #### C#
640+
641+ ``` cs
642+ /**
643+ * Definition for singly-linked list.
644+ * public class ListNode {
645+ * public int val;
646+ * public ListNode next;
647+ * public ListNode(int val=0, ListNode next=null) {
648+ * this.val = val;
649+ * this.next = next;
650+ * }
651+ * }
652+ */
653+ public class Solution {
654+ public ListNode MergeTwoLists (ListNode list1 , ListNode list2 ) {
655+ ListNode dummy = new ListNode ();
656+ ListNode curr = dummy ;
657+ while (list1 != null && list2 != null ) {
658+ if (list1 .val <= list2 .val ) {
659+ curr .next = list1 ;
660+ list1 = list1 .next ;
661+ } else {
662+ curr .next = list2 ;
663+ list2 = list2 .next ;
664+ }
665+ curr = curr .next ;
666+ }
667+ curr .next = list1 == null ? list2 : list1 ;
668+ return dummy .next ;
669+ }
670+ }
671+ ```
672+
673+ #### Ruby
674+
675+ ``` rb
676+ # Definition for singly-linked list.
677+ # class ListNode
678+ # attr_accessor :val, :next
679+ # def initialize(val = 0, _next = nil)
680+ # @val = val
681+ # @next = _next
682+ # end
683+ # end
684+ # @ param {ListNode} list1
685+ # @ param {ListNode} list2
686+ # @ return {ListNode}
687+ def merge_two_lists (list1 , list2 )
688+ dummy = ListNode .new ()
689+ cur = dummy
690+ while list1 && list2
691+ if list1.val <= list2.val
692+ cur.next = list1
693+ list1 = list1.next
694+ else
695+ cur.next = list2
696+ list2 = list2.next
697+ end
698+ cur = cur.next
699+ end
700+ cur.next = list1 || list2
701+ dummy.next
702+ end
703+ ```
704+
606705#### PHP
607706
608707``` php
@@ -616,18 +715,15 @@ var mergeTwoLists = function (list1, list2) {
616715# $this->next = $next;
617716# }
618717# }
619-
620718class Solution {
621719 /**
622720 * @param ListNode $list1
623721 * @param ListNode $list2
624722 * @return ListNode
625723 */
626-
627724 function mergeTwoLists($list1, $list2) {
628725 $dummy = new ListNode(0);
629726 $current = $dummy;
630-
631727 while ($list1 != null && $list2 != null) {
632728 if ($list1->val <= $list2->val) {
633729 $current->next = $list1;
0 commit comments