Today, I solved the Merge K sorted Lists.
Description:
In this question, we are gonna merge several lists. And, the merged list should be sorted in order.
Solution:
This is the first part of solution. We will look into mergeLists() function later.
If the gived lists are empty, we return None.
While loop runs until the length of lists is over 1. We make the list for storing merged lists.
Since we are gonna merge the two lists at one time, we need a for loop to select the two lists.
We set the loop to move 2. For example, there are four lists: l1, l2, l3 ,l4.
Then we are gonna compare (l1, l2) and (l3, l4).
We are gonna check L[i+1] is over the length of lists or not.
And, we append the merged lists in order
mergeLists()
This function algorithms is basic one for merging two lists in order.
We make the dummy node, and pointer to that dummy node.
Until those two lists exist, we move the tail pointer to value to the smallest next value.
After loop, we should check the left values on l1 and l2.
And, we return dummy.next.
'LeetCode 🏔️ > Linked List' 카테고리의 다른 글
141. Linked List Cycle (0) | 2023.05.01 |
---|---|
143. Reorder List (0) | 2023.04.25 |
19. Remove Nth Node From End of List (1) | 2023.04.23 |
21. Merge Two Sorted Lists (0) | 2023.04.10 |
206. Reverse Linked List (0) | 2023.04.09 |