- Notifications
You must be signed in to change notification settings - Fork1.3k
python3 solution to #21#168
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
selenaxiao wants to merge1 commit intofishercoder1534:masterChoose a base branch fromselenaxiao:21_solution
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletionspython3/21.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# https://leetcode.com/problems/merge-two-sorted-lists/ | ||
# Iterative solution | ||
class Solution: | ||
def mergeTwoLists(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]: | ||
tempHead = ListNode() # Empty node to be head of merged list | ||
temp = tempHead # End node of merged list, append new nodes to end of temp | ||
# While BOTH l1 and l2 still have nodes | ||
while (l1 is not None and l2 is not None): | ||
# If current node of l1 has a smaller value than current node of l2 | ||
if (l1.val < l2.val): | ||
# Append l1 node to merged list | ||
temp.next = l1 | ||
# Go to next l1 node | ||
l1 = l1.next | ||
else: | ||
temp.next = l2 | ||
l2 = l2.next | ||
# Go to new end of merged list | ||
temp = temp.next | ||
# Either l1, l2, or neither have nodes left | ||
# Can directly append all of the remaining nodes to merged list | ||
temp.next = l1 if l1 is not None else l2 | ||
# tempHead is empty node, return the actual head node of merged list | ||
return tempHead.next | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.