ListNode' object is not subscriptable
时间: 2023-09-30 10:02:06 浏览: 79
ListNode.java
This error occurs when you try to access an element of a ListNode object using the square bracket notation, but the object does not support subscripting.
For example, if you have a ListNode object like this:
```
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
```
And you try to access an element using the square bracket notation like this:
```
node = ListNode(1)
print(node[0])
```
You will get the error "TypeError: 'ListNode' object is not subscriptable" because ListNode does not support subscripting.
To access the values of a ListNode object, you can use a loop to iterate over the nodes:
```
node = ListNode(1)
while node is not None:
print(node.val)
node = node.next
```
阅读全文