#๐Ÿ”’ Merge Two Sorted Linked List

5 messages ยท Page 1 of 1 (latest)

odd monolith
#

class Solution(object):
def mergeTwoLists(self, list1, list2):
"""
:type list1: Optional[ListNode]
:type list2: Optional[ListNode]
:rtype: Optional[ListNode]
"""

    dummy = ListNode()
    tail = dummy

    while(list1 is not None and list2 is not None):
        if list1.val <= list2.val:
            tail.next = list1
            list1 = list1.next
        
        else:
            tail.next = list2
            list2 = list2.next
        
        tail = tail.next
    
    
    if list1 is not None:
        tail.next = list1
    
    elif list2 is not None:
        tail.next = list2
    
    return dummy.next

Anyone know why tail.next = list appends 1 node tail while the last if statement can append the remaining nodes? I'm so confused on this part

granite elbowBOT
#

@odd monolith

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

#

Hey @odd monolith!

It looks like you are trying to paste code into this channel.

You seem to be using the wrong symbols to indicate where the code block should start. The correct symbols would be ```, not """.

Here is an example of how it should look:
```
Hello, world!
```

This will result in the following:

Hello, world!```
You can **edit your original message** to correct your code block.
granite elbowBOT
#

@odd monolith

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.