Skip to main content
 首页 » 编程设计

python之while i 和 j/while j 和 i 在 python 中的区别

2025年01月19日10shangdawei

<分区>

我正在尝试做 leetcode #83 我不明白的是以下两种方式有什么区别:

    while cur and cur.next: 
 
    while cur.next and cur: 

如果我尝试第二种方式,会出现编译错误。 谁能帮助我理解这个概念?

    # Definition for singly-linked list. 
    # class ListNode(object): 
    #     def __init__(self, x): 
    #         self.val = x 
    #         self.next = None 
 
    class Solution(object): 
        def deleteDuplicates(self, head): 
            """ 
            :type head: ListNode 
    :rtype: ListNode 
    """ 
    cur= head 
    while cur and cur.next: 
        if cur.val== cur.next.val: 
            cur.next= cur.next.next 
        else:  
            cur = cur.next 
 
    return(head) 

我是数据结构的初学者。 我仍然困惑为什么 cur.next 是错误的方式。代码 cur = head 不起作用?