ListNode' object is not subscriptable
时间: 2023-09-30 14:02:06 浏览: 58
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
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)