C#
C++
Go
Java
Python
JavaScript
/**
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public static int GetVal(ListNode node) {
if (node == null) {
return int.MaxValue ;
}
return node.val;
}
public static ListNode MergeTwoLists(ListNode head1, ListNode head2) {
ListNode curr = new ListNode(0);
ListNode stab = curr;
while (head1 != null || head2 != null) {
if (GetVal(head1) < GetVal(head2)) {
curr.next = head1;
head1 = head1.next;
} else {
curr.next = head2;
head2 = head2.next;
}
curr = curr.next;
}
return stab.next;
}
}
#include <vector>
using namespace std;
/**
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
int getVal(ListNode* node) {
if (node == nullptr) {
return INT_MAX;
}
return node->val;
}
ListNode* mergeTwoLists(ListNode* head1, ListNode* head2) {
ListNode* curr = new ListNode(0);
ListNode* stab = curr;
while (head1 != nullptr || head2 != nullptr) {
if (getVal(head1) < getVal(head2)) {
curr->next = head1;
head1 = head1->next;
} else {
curr->next = head2;
head2 = head2->next;
}
curr = curr->next;
}
ListNode* result = stab->next;
delete stab; // Освобождаем память от вспомогательного узла
return result;
}
package main
/**
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func mergeTwoLists(head1 *ListNode, head2 *ListNode) *ListNode {
curr := &ListNode{}
stab := curr
for head1 != nil || head2 != nil {
if head1 != nil && (head2 == nil || head1.Val < head2.Val) {
curr.Next = head1
head1 = head1.Next
} else {
curr.Next = head2
head2 = head2.Next
}
curr = curr.Next
}
return stab.Next
}
import java.util.*;
/**
* class ListNode {
* Integer val;
* ListNode next;
* ListNode() {}
* ListNode(Integer val) { this.val = val; }
* ListNode(Integer val, ListNode next) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public int getVal(ListNode node) {
if (node == null) {
return Integer.MAX_VALUE;
}
return node.val;
}
public ListNode mergeTwoLists(ListNode head1, ListNode head2) {
ListNode curr = new ListNode();
ListNode stab = curr;
while (head1 != null || head2 != null) {
if (getVal(head1) < getVal(head2)) {
curr.next = head1;
head1 = head1.next;
} else {
curr.next = head2;
head2 = head2.next;
}
curr = curr.next;
}
return stab.next;
}
}
from list_node import ListNode
# class ListNode:
# def __init__(self, val, next=None):
# self.val = val
# self.next = next
def get_val(node: ListNode):
if node is None:
return float('inf')
return node.val
def merge_two_lists(head1: Optional[ListNode], head2: Optional[ListNode]) -> Optional[ListNode]:
curr = stab = ListNode(0)
while head1 is not None or head2 is not None:
if get_val(head1) < get_val(head2):
curr.next = head1
head1 = head1.next
else:
curr.next = head2
head2 = head2.next
curr = curr.next
return stab.next
import { ListNode } from './listNode.js';
/**
* class ListNode {
* constructor(val, next) {
* this.val = val;
* this.next = (next === undefined ? null : next);
* }
* }
*/
/**
* @param {ListNode} head1
* @param {ListNode} head2
* @returns {ListNode}
*/
export function mergeTwoLists(head1, head2) {
const getVal = (node) => {
if (node === null) {
return Infinity;
}
return node.val;
};
let curr = new ListNode();
let stab = curr;
while (head1 !== null || head2 !== null) {
if (getVal(head1) < getVal(head2)) {
curr.next = head1;
head1 = head1.next;
} else {
curr.next = head2;
head2 = head2.next;
}
curr = curr.next;
}
return stab.next;
}