Skip to content

Instantly share code, notes, and snippets.

@mlivingston40
Created March 1, 2024 18:47
Show Gist options
  • Save mlivingston40/a93aa091ab39486c80a44ba323f1d0ba to your computer and use it in GitHub Desktop.
Save mlivingston40/a93aa091ab39486c80a44ba323f1d0ba to your computer and use it in GitHub Desktop.
Merge k sorted lists
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def mergeKLists(self, lists: List[Optional[ListNode]]) -> List:
out_list = []
for l in lists:
while l is not None:
out_list.append(l.val)
l = l.next
# sort it in place
out_list.sort()
return out_list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment