pycharm里面不支持链表这种数据类型怎么破?
有些leetcode上的题很多是设计链表,二叉树的,这种题是不是用python做不了啊
python 支持,可以参考我写的解题过程
老师你说的是像这种重新定义一个类吗? 但是我试过这样写在pycharm里面也跑不通。。。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head or not head.next: return False
slow = head
fast = head.next
while slow != fast:
if not fast or not fast.next:
return False
slow = slow.next
fast = fast.next.next
return True
这种需要,自己定义数据结构
不知道怎么定义数据结构,有时候从大牛那直接拷过来在pycharm上也跑不了。。
建议你去看看算法的录播课,基本的数据结构都讲了
你把数据结构的代码注释了,取消注释:
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def hasCycle(self, head: ListNode) -> bool:
if not head or not head.next: return False
slow = head
fast = head.next
while slow != fast:
if not fast or not fast.next:
return False
slow = slow.next
fast = fast.next.next
return True
老师,总是报这个错
建议同学看看 Python 语法再刷题,这个就不是 pycharm 的问题了