Skip to content

Commit 2e5299e

Browse files
solves merge 2 sorted lists in python
1 parent 8a968e6 commit 2e5299e

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
| 13 | [Roman To Integer](https://leetcode.com/problems/roman-to-integer/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RomanToInteger.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/roman_to_integer.py) |
1515
| 14 | [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/LongestCommonPrefix.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/longest_common_prefix.py) |
1616
| 20 | [ValidParentheses](https://leetcode.com/problems/valid-parentheses/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/ValidParentheses.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/valid_parentheses.py) |
17-
| 21 | [Merge 2 Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Merge2SortedLists.java) |
17+
| 21 | [Merge 2 Sorted Lists](https://leetcode.com/problems/merge-two-sorted-lists/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/Merge2SortedLists.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/merge_2_sorted_lists.py) |
1818
| 26 | [Remove Duplicates From Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RemoveDuplicatesFromSortedArray.java) |
1919
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/RemoveElement.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/remove_element.py)|
2020
| 28 | [Needle in Haystack](https://leetcode.com/problems/implement-strstr) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](src/NeedleInHaystack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](python/needle_in_haystack.py) |

python/merge_2_sorted_lists.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Definition for singly-linked list.
2+
class ListNode:
3+
def __init__(self, val=0, next=None):
4+
self.val = val
5+
self.next = next
6+
7+
8+
class Solution:
9+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
10+
result = ListNode(val=-1)
11+
previous = result
12+
13+
while l1 is not None and l2 is not None:
14+
if l1.val <= l2.val:
15+
previous.next = l1
16+
l1 = l1.next
17+
else:
18+
previous.next = l2
19+
l2 = l2.next
20+
previous = previous.next
21+
22+
previous.next = l2 if l1 is None else l1
23+
return result.next

0 commit comments

Comments
 (0)