Skip to content

Commit 520ad81

Browse files
Merge pull request #2972 from coopers/0023
Update 0023-merge-k-sorted-lists.py
2 parents 2eb4ff8 + 6086339 commit 520ad81

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

python/0023-merge-k-sorted-lists.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@ def mergeKLists(self, lists: List[ListNode]) -> ListNode:
1818
return lists[0]
1919

2020
def mergeList(self, l1, l2):
21-
dummy = ListNode()
22-
tail = dummy
21+
dummy = node = ListNode()
2322

2423
while l1 and l2:
2524
if l1.val < l2.val:
26-
tail.next = l1
25+
node.next = l1
2726
l1 = l1.next
2827
else:
29-
tail.next = l2
28+
node.next = l2
3029
l2 = l2.next
31-
tail = tail.next
32-
if l1:
33-
tail.next = l1
34-
if l2:
35-
tail.next = l2
30+
node = node.next
31+
32+
node.next = l1 or l2
3633
return dummy.next

0 commit comments

Comments
 (0)