Commit 84cf55a
committed
21. Merge Two Sorted Lists
```Solution.c /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) { struct ListNode* merged = NULL; struct ListNode** lastPtr = &merged; while (list1 && list2) { struct ListNode* newNode = malloc(sizeof(struct ListNode)); if (!newNode) { return NULL; } if (list1->val < list2->val) { newNode->val = list1->val; list1 = list1->next; } else { newNode->val = list2->val; list2 = list2->next; } newNode->next = NULL; *lastPtr = newNode; lastPtr = &newNode->next; } *lastPtr = list1 ? list1 : list2; return merged; } ``` ```Solution.cpp /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { ListNode* merged = nullptr; ListNode** lastPtr = &merged; while (list1 && list2) { ListNode* newNode = new ListNode(); if (!newNode) { return nullptr; } if (list1->val < list2->val) { newNode->val = list1->val; list1 = list1->next; } else { newNode->val = list2->val; list2 = list2->next; } newNode->next = nullptr; *lastPtr = newNode; lastPtr = &newNode->next; } *lastPtr = list1 ? list1 : list2; return merged; } }; ``` ```Solution.cs /** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int val=0, ListNode next=null) { * this.val = val; * this.next = next; * } * } */ public class Solution { public ListNode MergeTwoLists(ListNode list1, ListNode list2) { ListNode merged = new ListNode(0); ListNode current = merged; while (list1 != null && list2 != null) { if (list1.val < list2.val) { current.next = list1; list1 = list1.next; } else { current.next = list2; list2 = list2.next; } current = current.next; } current.next = list1 ?? list2; return merged.next; } } ``` ```Solution.dart /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode? next; * ListNode([this.val = 0, this.next]); * } */ class Solution { ListNode? mergeTwoLists(ListNode? list1, ListNode? list2) { ListNode? merged = ListNode(); ListNode? current = merged; while (list1 != null && list2 != null) { if (list1.val < list2.val) { current?.next = list1; list1 = list1.next; } else { current?.next = list2; list2 = list2.next; } current = current?.next; } current?.next = list1 ?? list2; return merged?.next; } } ``` ```Solution.erl %% Definition for singly-linked list. %% %% -record(list_node, {val = 0 :: integer(), %% next = null :: 'null' | #list_node{}}). -spec merge_two_lists(List1 :: #list_node{} | null, List2 :: #list_node{} | null) -> #list_node{} | null. merge_two_lists(null, List2) -> List2; merge_two_lists(List1, null) -> List1; merge_two_lists(List1 = #list_node{val = V1, next = Next1}, List2 = #list_node{val = V2, next = Next2}) -> if V1 =< V2 -> #list_node{val = V1, next = merge_two_lists(Next1, List2)}; true -> #list_node{val = V2, next = merge_two_lists(List1, Next2)} end. ``` ```Solution.ex # Definition for singly-linked list. # # defmodule ListNode do # @type t :: %__MODULE__{ # val: integer, # next: ListNode.t() | nil # } # defstruct val: 0, next: nil # end defmodule Solution do @SPEC merge_two_lists(list1 :: ListNode.t | nil, list2 :: ListNode.t | nil) :: ListNode.t | nil def merge_two_lists(nil, list2), do: list2 def merge_two_lists(list1, nil), do: list1 def merge_two_lists(%{val: v1, next: n1} = list1, %{val: v2, next: n2} = list2) do if v1 <= v2 do %ListNode{val: v1, next: merge_two_lists(n1, list2)} else %ListNode{val: v2, next: merge_two_lists(list1, n2)} end end end ``` ```Solution.go /** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ func mergeTwoLists(list1 *ListNode, list2 *ListNode) *ListNode { if list1 == nil { return list2 } if list2 == nil { return list1 } if list1.Val < list2.Val { list1.Next = mergeTwoLists(list1.Next, list2) return list1 } list2.Next = mergeTwoLists(list1, list2.Next) return list2 } ``` ```Solution.java /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode dummy = new ListNode(0); ListNode current = dummy; while (list1 != null && list2 != null) { if (list1.val < list2.val) { current.next = list1; list1 = list1.next; } else { current.next = list2; list2 = list2.next; } current = current.next; } if (list1 != null) { current.next = list1; } else { current.next = list2; } return dummy.next; } } ``` ```Solution.js /** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } */ /** * @param {ListNode} list1 * @param {ListNode} list2 * @return {ListNode} */ var mergeTwoLists = function(list1, list2) { if (!list1) return list2; if (!list2) return list1; if (list1.val < list2.val) { list1.next = mergeTwoLists(list1.next, list2); return list1; } else { list2.next = mergeTwoLists(list1, list2.next); return list2; } }; ``` ```Solution.kt /** * Example: * var li = ListNode(5) * var v = li.`val` * Definition for singly-linked list. * class ListNode(var `val`: Int) { * var next: ListNode? = null * } */ class Solution { fun mergeTwoLists(list1: ListNode?, list2: ListNode?): ListNode? { val dummy = ListNode(0) var tail = dummy var l1 = list1 var l2 = list2 while (l1 != null && l2 != null) { if (l1.`val` < l2.`val`) { tail.next = l1 l1 = l1.next } else { tail.next = l2 l2 = l2.next } tail = tail.next!! } tail.next = l1 ?: l2 return dummy.next } } ``` ```Solution.php /** * Definition for a singly-linked list. * class ListNode { * public $val = 0; * public $next = null; * function __construct($val = 0, $next = null) { * $this->val = $val; * $this->next = $next; * } * } */ class Solution { /** * @param ListNode $list1 * @param ListNode $list2 * @return ListNode */ function mergeTwoLists($list1, $list2) { $dummy = new ListNode(0); $current = $dummy; while ($list1 !== null && $list2 !== null) { if ($list1->val < $list2->val) { $current->next = $list1; $list1 = $list1->next; } else { $current->next = $list2; $list2 = $list2->next; } $current = $current->next; } if ($list1 !== null) { $current->next = $list1; } else { $current->next = $list2; } return $dummy->next; } } ``` ```Solution.py # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]: if not list1: return list2 if not list2: return list1 if list1.val <= list2.val: list1.next = self.mergeTwoLists(list1.next, list2) return list1 else: list2.next = self.mergeTwoLists(list1, list2.next) return list2 ``` ```Solution.rb # Definition for singly-linked list. # class ListNode # attr_accessor :val, :next # def initialize(val = 0, _next = nil) # @Val = val # @next = _next # end # end # @param {ListNode} list1 # @param {ListNode} list2 # @return {ListNode} def merge_two_lists(list1, list2) dummy = ListNode.new current = dummy while list1 && list2 if list1.val < list2.val current.next = list1 list1 = list1.next else current.next = list2 list2 = list2.next end current = current.next end current.next = list1 || list2 dummy.next end ``` ```Solution.rkt ; Definition for singly-linked list: #| ; val : integer? ; next : (or/c list-node? #f) (struct list-node (val next) #:mutable #:transparent) ; constructor (define (make-list-node [val 0]) (list-node val #f)) |# (define/contract (merge-two-lists list1 list2) (-> (or/c list-node? #f) (or/c list-node? #f) (or/c list-node? #f)) (cond [(not list1) list2] [(not list2) list1] [(<= (list-node-val list1) (list-node-val list2)) (set-list-node-next! list1 (merge-two-lists (list-node-next list1) list2)) list1] [else (set-list-node-next! list2 (merge-two-lists list1 (list-node-next list2))) list2])) ``` ```Solution.rs use crate::list_node::ListNode; impl Solution { pub fn merge_two_lists( mut list1: Option<Box<ListNode>>, mut list2: Option<Box<ListNode>>, ) -> Option<Box<ListNode>> { let mut dummy = Box::new(ListNode { val: 0, next: None }); let mut tail = &mut dummy; while list1.is_some() && list2.is_some() { let l1_val = list1.as_ref().unwrap().val; let l2_val = list2.as_ref().unwrap().val; if l1_val < l2_val { let next = list1.as_mut().unwrap().next.take(); tail.next = list1; tail = tail.next.as_mut().unwrap(); list1 = next; } else { let next = list2.as_mut().unwrap().next.take(); tail.next = list2; tail = tail.next.as_mut().unwrap(); list2 = next; } } tail.next = if list1.is_some() { list1 } else { list2 }; dummy.next } } ``` ```Solution.scala /** * Definition for singly-linked list. * class ListNode(_x: Int = 0, _next: ListNode = null) { * var next: ListNode = _next * var x: Int = _x * } */ object Solution { def mergeTwoLists(list1: ListNode, list2: ListNode): ListNode = { if (list1 == null) return list2 if (list2 == null) return list1 if (list1.x < list2.x) { list1.next = mergeTwoLists(list1.next, list2) list1 } else { list2.next = mergeTwoLists(list1, list2.next) list2 } } } ``` ```Solution.swift /** * Definition for singly-linked list. * public class ListNode { * public var val: Int * public var next: ListNode? * public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val; self.next = nil; } * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; } * } */ class Solution { func mergeTwoLists(_ list1: ListNode?, _ list2: ListNode?) -> ListNode? { let dummy = ListNode(0) var current = dummy var l1 = list1 var l2 = list2 while l1 != nil && l2 != nil { if l1!.val < l2!.val { current.next = l1 l1 = l1!.next } else { current.next = l2 l2 = l2!.next } current = current.next! } current.next = l1 ?? l2 return dummy.next } } ``` ```Solution.ts /** * Definition for singly-linked list. * class ListNode { * val: number * next: ListNode | null * constructor(val?: number, next?: ListNode | null) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } * } */ function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { if (!list1) return list2; if (!list2) return list1; if (list1.val < list2.val) { list1.next = mergeTwoLists(list1.next, list2); return list1; } else { list2.next = mergeTwoLists(list1, list2.next); return list2; } }; ```1 parent 14de6d5 commit 84cf55a
File tree
20 files changed
+494
-0
lines changed- res/ino
- sol/solution/0001-0100/0021
20 files changed
+494
-0
lines changedLoading
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
0 commit comments