Python while list 1 and list 2 are incomprehensible.

Asked 1 years ago, Updated 1 years ago, 276 views

 class solution:
defaultTwoLists(self, list1: Optional [ListNode], list2: Optional [ListNode] - > Optional [ListNode]:
    cur=dummy=ListNode()
    while list1 and list2:               
        if list1.val<list2.val:
            cur.next=list1
            list1, cur=list1.next, list1
        else:
            cur.next=list2
            list2, cur=list2.next, list2
            
    if list1 or list2:
        cur.next=list1 if list1 else list2
        
    return dummy.next

Merge consolidated lists sorted by leetcode.
A consolidated list with list1 and list2.
Sorry for the rudimentary question, but what does while list1 and list2: mean?
Do you mean list1 and list2 are Boolean values?

python

2023-02-12 19:59

1 Answers

From docs.python.org embedded

truth determination
Any object can be determined as a true value and can be used as a condition for if or while or as an operator for the following Boolean operations:
The object is determined to be true by default.However, if the class defines the __bool__() method and it returns False, or if it defines the _len__() method and it returns 0, it is considered false.Here are some of the key embedded objects that are considered false:

false:
  • constants defined as false: None and False
  • Zero in numeric form: 0, 0.0, 0j, Decimal(0), Fraction(0,1)
  • empty sequence or collection:', (), [ ], {}, set(), range(0)

boole operation---and, or, not
The Boolean operations are listed in descending order of priority:


as described above Some objects determine False

In the case of questions, according to the definition of ListNode(), if the linked list does not continue, it is considered false


2023-02-12 22:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.