favorite We need a little bit of your help to keep things running, click on this banner to learn more

class LinkedList

LinkedList Merge

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Definition of a single linked list:

// Java
class ListNode {
  int val;
  ListNode next;
  ListNode(int x) {
    val = x;
    next = null;
// C++
class ListNode
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}

Implement function merge that merges two linked lists.

// Java
ListNode merge(ListNode l1, ListNode l2)
// C++
ListNode* merge(ListNode *l1, ListNode *l2)



Time limit 1 second
Memory limit 128 MiB
Author Mykhailo Medvediev