Skip to content

📦 NEW: merge_two_shorted_list #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions 021_Merge_Two_Shorted_List.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None

def __repr__(self):
if self:
return "{} -> {}".format(self.val, self.next)


class Solution:
def mergeTwoLists(self, l1, l2):
dummy = ListNode(0)
current = dummy

while l1 and l2:
if l1.val < l2.val:
current.next = l1
l1 = l1.next
else:
current.next = l2
l2 = l2.next
current = current.next

if l1:
current.next = l1
else:
current.next = l2

return dummy.next


if __name__ == "__main__":
l1 = ListNode(0)
l1.next = ListNode(1)
l2 = ListNode(2)
l2.next = ListNode(3)
print(Solution().mergeTwoLists(l1, l2))