Skip to content

Commit 6c0ee11

Browse files
committed
Adding Simple Solution for Problem - 23 - Merge K Sorted Lists
1 parent a22814e commit 6c0ee11

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Merge_K_Sorted_List/SimpleSolution.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

0 commit comments

Comments
 (0)