TypeError: 'ListNode' object is not iterable in K Reverse Linked List question
TypeError: 'ListNode' object is not iterable in K Reverse Linked List question
我正在 InterviewBit 上练习链表题。这里的问题是
'给定一个单链表和一个整数K,反转链表的节点
一次列出 K 和 returns 修改链表。
注意:列表的长度可以被 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
有 return A
(一个值)和 return self.head, cur
(元组)。这是不一致的。在你调用这个函数的地方,你期望一个元组,所以第一个 return
是错误的。实际上,if
阻塞发生的地方,可以被删除
return start
的缩进少了一个space(可能只是问题的错别字)
赋值 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
.
即使模板代码可能建议使用变量 A
和 B
,最好使用更具描述性的变量,例如 head
和 count
.然后在 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)
我正在 InterviewBit 上练习链表题。这里的问题是 '给定一个单链表和一个整数K,反转链表的节点 一次列出 K 和 returns 修改链表。
注意:列表的长度可以被 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
有return A
(一个值)和return self.head, cur
(元组)。这是不一致的。在你调用这个函数的地方,你期望一个元组,所以第一个return
是错误的。实际上,if
阻塞发生的地方,可以被删除return start
的缩进少了一个space(可能只是问题的错别字)赋值
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
.即使模板代码可能建议使用变量
A
和B
,最好使用更具描述性的变量,例如head
和count
.然后在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)