Skip to content

Instantly share code, notes, and snippets.

@potatowagon
Forked from krishnadey30/LeetCodeQuestions.md
Last active October 20, 2021 11:14
Show Gist options
  • Save potatowagon/d776c097694fc9221372d8cf5a950cf3 to your computer and use it in GitHub Desktop.
Save potatowagon/d776c097694fc9221372d8cf5a950cf3 to your computer and use it in GitHub Desktop.
Curated List of Top 75 LeetCode

Array


Binary


Dynamic Programming


Graph


Interval


Linked List


Matrix


String


Tree


Heap

string convertions and validations

  • String to Integer (atoi)
  • Integer to English Words
  • Valid Number

Important Link:

14 Patterns to Ace Any Coding Interview Question

@potatowagon
Copy link
Author

Merge K Sorted Lists

class Solution:
    def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:
        heap = []
        for idx, headlistnode in enumerate(lists):
            if headlistnode:
                heap.append((headlistnode.val, idx))
        
        # sort asc accourding to list node val
        heapq.heapify(heap)
        # dummy node
        head = ListNode()
        cur = head
        while heap:
            min_val, idx = heapq.heappop(heap)
            new_node = ListNode(min_val)
            cur.next = new_node
            cur = cur.next
            
            # push next val from idx list onto heap
            push_node = lists[idx] = lists[idx].next
            if push_node:
                heapq.heappush(heap, (push_node.val, idx))
        return head.next

@potatowagon
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment