【问题标题】:TypeError: 'ListNode' object is not iterable in K Reverse Linked List questionTypeError:“ListNode”对象在 K 反向链接列表问题中不可迭代
【发布时间】:2021-12-30 07:32:12
【问题描述】:

我正在 InterviewBit 上练习链表问题。这里的问题是 '给定一个单链表和一个整数 K,反转 一次列出 K 并返回修改后的链表。

注意:列表的长度可以被 K' 整除

遵循一种天真的方法,这就是我所做的:

# Definition for singly-linked list.
# class ListNode:
#   def __init__(self, x):
#       self.val = x
#       self.next = None

class Solution:
    def reversesubList(self, A, B):
        prev = None 
        cur = A
        if cur.next is None:
            return A 

        count = 0 
        while(cur is not None) and count<B:
            nxt = cur.next 
            cur.next = prev 
            prev = cur 
            cur = nxt 
            count += 1

        self.head = prev 
        return self.head, cur
    # @param A : head node of linked list
    # @param B : integer
    # @return the head node in the linked list
    def reverseList(self, A, B):
        current = A
        last_of_prev = None 
        count = 0
        while current is not None:
            reversed_head, new_head = self.reversesubList(current, B)
            # print(reversed_head.val)
            # print(new_head.val)
            if count>0: 
                last_of_prev.next = reversed_head
            else: 
                start = reversed_head
                last_of_prev = current
            current.next = new_head
            current = new_head
            count += 1  
       return start

我们的想法是遍历列表并通过一次切换每个考虑的 B 元素集的指针,一次通过我们应该能够解决问题。我收到以下错误,但无法查明原因:

Traceback (most recent call last):
  File "main.py", line 228, in <module>
    Z = obj.reverseList(A, B)
  File "/tmp/judge/solution.py", line 25, in reverseList
    reversed_head, new_head = self.reversesubList(current, B)
TypeError: 'ListNode' object is not iterable

任何可以让我理解错误的帮助将不胜感激,谢谢!

【问题讨论】:

  • reversesubList() 返回单个 ListNode 但您的调用代码需要两个值

标签: python python-3.x linked-list


【解决方案1】:

有这些问题:

  • reversesubListreturn A(一个值)和return self.head, cur(元组)。这是不一致的。在你调用这个函数的地方,你期望一个元组,所以第一个 return 是错误的。实际上,它发生的if 块可以被删除

  • return start 的缩进少了一个空格(可能只是问题中的错字)

  • 分配last_of_prev = current 不仅应该发生在else 的情况下,而且总是。所以将其移出else 正文。

没有问题,但可以改进以下几点:

  • count 变量似乎有点矫枉过正,因为它仅用于查看它是否是循环的第一次迭代。为此,您可以使用if last_of_prev——因此不需要count 变量。

  • reversesubList 中应该不需要分配给self.head——它永远不会被读取。不用那个任务就做return prev, nxt

  • current.next = new_head 并不是真正需要的,因为从new_head 开始的(剩余)列表仍然需要在循环的下一次迭代中反转,所以无论如何都需要更正这个分配,这发生在分配(在下一次迭代中)到last_of_prev.next

  • 即使模板代码可能有建议的变量AB,最好使用描述性更强的变量,例如headcount。然后在reversesubList 函数中,您可以减少给定的count,而无需其他变量。

综合所有这些,我们得到了这个:

class ListNode:
    def __init__(self, x, nxt=None):
        self.val = x
        self.next = nxt

    # Some helper methods to be able to test outside
    #    the code-challenge site's framework
    @classmethod
    def of(Cls, values):
        head = None
        for value in reversed(values):
            head = Cls(value, head)
        return head

    def __iter__(self):
        head = self
        while head:
            yield head.val
            head = head.next

class Solution:
    def reversesubList(self, head, count):
        prev = None 
        cur = head

        while cur is not None and count > 0:
            nxt = cur.next 
            cur.next = prev 
            prev = cur 
            cur = nxt 
            count -= 1

        return prev, cur

    def reverseList(self, head, count):
        current = head
        last_of_prev = None 
        start = None
        while current is not None:
            reversed_head, new_head = self.reversesubList(current, count)
            if last_of_prev: 
                last_of_prev.next = reversed_head
            else: 
                start = reversed_head
            last_of_prev = current
            current = new_head
        return start

# Test
lst = ListNode.of([1,2,3,4,5,6,7,8,9])
print(*lst)

lst = Solution().reverseList(lst, 3)
print(*lst)

【讨论】:

  • 这非常有帮助和详尽,谢谢!很抱歉没有早点回来。
猜你喜欢
  • 1970-01-01
  • 2015-10-23
  • 2018-11-19
  • 2020-02-06
  • 2022-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 2022-01-06
相关资源
最近更新 更多