File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change
1
+ ##==================================
2
+ ## Leetcode
3
+ ## Student: Vandit Jyotindra Gajjar
4
+ ## Year: 2020
5
+ ## Problem: 23
6
+ ## Problem Name: Merge k Sorted Lists
7
+ ##===================================
8
+ #
9
+ #Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
10
+ #
11
+ #Example:
12
+ #
13
+ #Input:
14
+ #[
15
+ # 1->4->5,
16
+ # 1->3->4,
17
+ # 2->6
18
+ #]
19
+ #Output: 1->1->2->3->4->4->5->6
20
+ class Solution :
21
+ def mergeKLists (self , lists ):
22
+ tmp = [] #Initialize tmp empty list
23
+ dummy = head = ListNode (0 ) #Initialize dummy linkedlist
24
+ for i in lists : #Loop through lists
25
+ while i : #Loop
26
+ tmp .append (i .val ) #Append values in tmp list
27
+ i = i .next #Traverse through list
28
+ for i in sorted (tmp ): #Loop through sorted tmp list
29
+ dummy .next = ListNode (i ) #Add the values in dummy linkedlist
30
+ dummy = dummy .next #Point reference to next node
31
+ return head .next #Return our sorted dummy list
You can’t perform that action at this time.
0 commit comments