mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
更新 0203.移除链表元素.md python代码
使用更符合PEP8要求的代码规范来约束
This commit is contained in:
@ -245,13 +245,15 @@ Python:
|
|||||||
# def __init__(self, val=0, next=None):
|
# def __init__(self, val=0, next=None):
|
||||||
# self.val = val
|
# self.val = val
|
||||||
# self.next = next
|
# self.next = next
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
def removeElements(self, head: ListNode, val: int) -> ListNode:
|
def removeElements(self, head: ListNode, val: int) -> ListNode:
|
||||||
dummy_head = ListNode(next=head) #添加一个虚拟节点
|
dummy_head = ListNode(next=head)
|
||||||
cur = dummy_head
|
cur = dummy_head
|
||||||
while(cur.next!=None):
|
|
||||||
if(cur.next.val == val):
|
while cur.next:
|
||||||
cur.next = cur.next.next #删除cur.next节点
|
if cur.next.val == val:
|
||||||
|
cur.next = cur.next.next # 删除下一个节点
|
||||||
else:
|
else:
|
||||||
cur = cur.next
|
cur = cur.next
|
||||||
return dummy_head.next
|
return dummy_head.next
|
||||||
|
Reference in New Issue
Block a user