{"id":22513663,"url":"https://github.com/emahtab/merge-two-sorted-lists","last_synced_at":"2026-02-03T01:38:06.775Z","repository":{"id":79525669,"uuid":"240340560","full_name":"eMahtab/merge-two-sorted-lists","owner":"eMahtab","description":"Merge Two Sorted Linked Lists","archived":false,"fork":false,"pushed_at":"2020-05-18T07:32:30.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-07T20:06:33.077Z","etag":null,"topics":["leetcode","linked-list","merge-sorted-lists","min-heap","priority-queue","problem-solving"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/eMahtab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2020-02-13T19:11:56.000Z","updated_at":"2020-05-18T07:34:52.000Z","dependencies_parsed_at":"2023-05-10T17:30:27.877Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/merge-two-sorted-lists","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/merge-two-sorted-lists","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmerge-two-sorted-lists","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmerge-two-sorted-lists/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmerge-two-sorted-lists/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmerge-two-sorted-lists/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/merge-two-sorted-lists/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fmerge-two-sorted-lists/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262502610,"owners_count":23321168,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["leetcode","linked-list","merge-sorted-lists","min-heap","priority-queue","problem-solving"],"created_at":"2024-12-07T03:13:56.891Z","updated_at":"2026-02-03T01:38:06.737Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Merge Two Sorted Linked Lists\n# https://leetcode.com/problems/merge-two-sorted-lists\n\nMerge 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.\n```\nExample:\n\nInput: 1-\u003e2-\u003e4, 1-\u003e3-\u003e4\nOutput: 1-\u003e1-\u003e2-\u003e3-\u003e4-\u003e4\n```\n\n# Implementation 1 :\n```java\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n        if(l1 == null || l2 == null)\n            return l1 == null ? l2 : l1;\n        \n        Queue\u003cInteger\u003e pq = new PriorityQueue\u003c\u003e();\n        addNodes(l1, pq);\n        addNodes(l2, pq);\n      \n        ListNode head = null;\n        ListNode prev = null;\n        ListNode node = null;\n        \n        while(!pq.isEmpty()) {\n            node = new ListNode(pq.poll());\n            if(head == null)\n                head = node;\n            if(prev != null)\n                prev.next = node;\n            prev = node;\n        }\n       return head; \n    }\n    \n    private void addNodes(ListNode head, Queue\u003cInteger\u003e q) {\n        ListNode current = head;\n        while(current != null) {\n            q.add(current.val);\n            current = current.next;\n        }\n    }\n}\n```\n\n# Implementation 2 :\n\n```java\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution {\n    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {\n        if(l1 == null || l2 == null)\n            return l1 == null ? l2 : l1;\n        \n        ListNode p1 = l1, p2 = l2;\n        ListNode head = null;\n        ListNode prev = null;\n        ListNode node = null;\n        while(p1 != null \u0026\u0026 p2 != null) {\n            if(p1.val \u003c= p2.val) {\n                node = new ListNode(p1.val);\n                p1 = p1.next;\n            } else {\n                node = new ListNode(p2.val);\n                p2 = p2.next;\n            }\n            if(head == null)\n                head = node;\n            if(prev != null)\n                prev.next = node;\n            prev = node;\n        }\n        while(p1 != null){\n            node = new ListNode(p1.val);\n            prev.next = node;\n            prev = node;\n            p1 = p1.next;\n        }\n        while(p2 != null) {\n            node = new ListNode(p2.val);\n            prev.next = node;\n            prev = node;\n            p2 = p2.next;\n        }\n        \n       return head; \n    }\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmerge-two-sorted-lists","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fmerge-two-sorted-lists","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fmerge-two-sorted-lists/lists"}