`{"id":"d6470f7c-628c-4ad6-b678-32045b15b495","name":"Merge Two Sorted Linkedlist","description":"1. Merge two sorted linkedlists and return head of a sorted linkedlist. The list should be made by splicing together the nodes of the first two lists\r\n2. Both list are sorted in increasing order.\r\n","inputFormat":"1->2->6->7->15->24->null\r\n-1->0->6->17->25->null\r\n","outputFormat":"-1->0->1->2->6->6->7->15->17->24->25->null","constraints":"0 &lt;= size of linkedlist &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *mergeTwoLists(ListNode *l1, ListNode *l2)\r\n{\r\n return nullptr;\r\n}\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nint main()\r\n{\r\n int n, m;\r\n cin >> n ;\r\n ListNode *h1 = createList(n);\r\n cin >> m;\r\n ListNode *h2 = createList(m);\r\n\r\n ListNode *head = mergeTwoLists(h1, h2);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n int m = scn.nextInt();\r\n ListNode h2 = createList(m);\r\n\r\n\r\n ListNode head = mergeTwoLists(h1, h2);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2\r\n1 5 \r\n4\r\n1 3 6 10 \r\n","sampleOutput":"1 1 3 5 6 10 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"fbeb5118-ca22-4747-a3ce-68ce45eb02ce","name":"Merge Two Sorted Linkedlist","slug":"merge-two-sorted-linkedlist","type":1}],"next":{"id":"170568c4-1773-4180-b0e0-421b48905bb5","name":"Merge Two Sorted Linkedlist MCQ","type":0,"slug":"merge-two-sorted-linkedlist-mcq"},"prev":{"id":"1f8649a7-2f86-4a96-b5de-1ce0f07a8872","name":"Unfold of Linkedlist","type":3,"slug":"unfold-of-linkedlist"}}}`

1. Merge two sorted linkedlists and return head of a sorted linkedlist. The list should be made by splicing together the nodes of the first two lists 2. Both list are sorted in increasing order.

`{"id":"d6470f7c-628c-4ad6-b678-32045b15b495","name":"Merge Two Sorted Linkedlist","description":"1. Merge two sorted linkedlists and return head of a sorted linkedlist. The list should be made by splicing together the nodes of the first two lists\r\n2. Both list are sorted in increasing order.\r\n","inputFormat":"1->2->6->7->15->24->null\r\n-1->0->6->17->25->null\r\n","outputFormat":"-1->0->1->2->6->6->7->15->17->24->25->null","constraints":"0 &lt;= size of linkedlist &lt;= 10^6\r\n","sampleCode":{"cpp":{"code":"#include <iostream>\r\nusing namespace std;\r\n\r\nclass ListNode\r\n{\r\npublic:\r\n int val = 0;\r\n ListNode *next = nullptr;\r\n\r\n ListNode(int val)\r\n {\r\n this->val = val;\r\n }\r\n};\r\n\r\nListNode *mergeTwoLists(ListNode *l1, ListNode *l2)\r\n{\r\n return nullptr;\r\n}\r\nvoid printList(ListNode *node)\r\n{\r\n ListNode *curr = node;\r\n while (curr != nullptr)\r\n {\r\n cout << curr->val << \" \";\r\n curr = curr->next;\r\n }\r\n cout << endl;\r\n}\r\n\r\nListNode *createList(int n)\r\n{\r\n ListNode *dummy = new ListNode(-1);\r\n ListNode *prev = dummy;\r\n while (n-- > 0)\r\n {\r\n int val;\r\n cin >> val;\r\n prev->next = new ListNode(val);\r\n prev = prev->next;\r\n }\r\n return dummy->next;\r\n}\r\n\r\nint main()\r\n{\r\n int n, m;\r\n cin >> n ;\r\n ListNode *h1 = createList(n);\r\n cin >> m;\r\n ListNode *h2 = createList(m);\r\n\r\n ListNode *head = mergeTwoLists(h1, h2);\r\n printList(head);\r\n\r\n return 0;\r\n}"},"java":{"code":"import java.util.*;\r\n\r\nclass Main {\r\n public static Scanner scn = new Scanner(System.in);\r\n\r\n public static class ListNode {\r\n int val = 0;\r\n ListNode next = null;\r\n\r\n ListNode(int val) {\r\n this.val = val;\r\n }\r\n }\r\n\r\n public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {\r\n return null;\r\n }\r\n\r\n public static void printList(ListNode node) {\r\n while (node != null) {\r\n System.out.print(node.val + \" \");\r\n node = node.next;\r\n }\r\n }\r\n\r\n public static ListNode createList(int n) {\r\n ListNode dummy = new ListNode(-1);\r\n ListNode prev = dummy;\r\n while (n-- > 0) {\r\n prev.next = new ListNode(scn.nextInt());\r\n prev = prev.next;\r\n }\r\n\r\n return dummy.next;\r\n }\r\n\r\n public static void main(String[] args) {\r\n int n = scn.nextInt();\r\n ListNode h1 = createList(n);\r\n int m = scn.nextInt();\r\n ListNode h2 = createList(m);\r\n\r\n\r\n ListNode head = mergeTwoLists(h1, h2);\r\n printList(head);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"2\r\n1 5 \r\n4\r\n1 3 6 10 \r\n","sampleOutput":"1 1 3 5 6 10 ","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"1e4c8949-5890-4d15-be5b-6601c7e2029a","name":"Linked List For Intermediate","slug":"linked-list-for-intermediate-637","type":0},{"id":"fbeb5118-ca22-4747-a3ce-68ce45eb02ce","name":"Merge Two Sorted Linkedlist","slug":"merge-two-sorted-linkedlist","type":1}],"next":{"id":"170568c4-1773-4180-b0e0-421b48905bb5","name":"Merge Two Sorted Linkedlist MCQ","type":0,"slug":"merge-two-sorted-linkedlist-mcq"},"prev":{"id":"1f8649a7-2f86-4a96-b5de-1ce0f07a8872","name":"Unfold of Linkedlist","type":3,"slug":"unfold-of-linkedlist"}}}` Editor

easy

1. Merge two sorted linkedlists and return head of a sorted linkedlist. The list should be made by splicing together the nodes of the first two lists 2. Both list are sorted in increasing order.

## Constraints

0 <= size of linkedlist <= 10^6

## Format

### Input

1->2->6->7->15->24->null -1->0->6->17->25->null

### Output

-1->0->1->2->6->6->7->15->17->24->25->null

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}2 1 5 4 1 3 6 10 ```

### Sample Output

`.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}1 1 3 5 6 10 `

Discussions

Show Discussion

Related Resources 