Python中的链表定义
class ListNode:
def __init__(self, val, next=None):
self.val = val
self.next = next
调了一段时间,主要卡在边界条件的判断不严谨以及删除节点的时候没有用循环
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
if head == None:
return head
dummy_node = ListNode(next=head)
node = dummy_node
while node != None and node.next != None:
while node.next != None and node.next.val == val:
node.next = node.next.next
node = node.next
return dummy_node.next
示例代码如下,可以用current表示当前节点,并且修改的是current的next
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
# 创建虚拟头部节点以简化删除过程
dummy_head = ListNode(next = head)
# 遍历列表并删除值为val的节点
current = dummy_head
while current.next:
if current.next.val == val:
current.next = current.next.next
else:
current = current.next
return dummy_head.next
先给一开始的代码存个档如下,这里有些比较明显的问题
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.head = None
def get(self, index: int) -> int:
current = self.head
for _ in range(index):
if current != None:
current = current.next
if current == None:
return -1
else:
return current.val
def addAtHead(self, val: int) -> None:
dummy_node = self.head
dummy_node.next = ListNode(val=val, next=self.head)
self.head = dummy_node
def addAtTail(self, val: int) -> None:
dummy_node = ListNode(next=self.head)
while dummy_node.next != None:
dummy_node = dummy_node.next
dummy_node.next = ListNode(val=val, next=None)
def addAtIndex(self, index: int, val: int) -> None:
def deleteAtIndex(self, index: int) -> None:
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
下面附上代码随想录里面的Python示例代码,好好学习下
# 单链表法
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.dummy_head = ListNode()
self.size = 0
def get(self, index: int) -> int:
if index < 0 or index >= self.size:
return -1
current = self.dummy_head.next
for i in range(index):
current = current.next
return current.val
def addAtHead(self, val: int) -> None:
self.dummy_head.next = ListNode(val, self.dummy_head.next)
self.size += 1
def addAtTail(self, val: int) -> None:
current = self.dummy_head
while current.next:
current = current.next
current.next = ListNode(val)
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index < 0 or index > self.size:
return
current = self.dummy_head
for i in range(index):
current = current.next
current.next = ListNode(val, current.next)
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
current = self.dummy_head
for i in range(index):
current = current.next
current.next = current.next.next
self.size -= 1
# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)
看了卡哥讲解的视频,然后根据伪代码写的
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur = head
pre = None
while cur:
temp = cur.next
cur.next = pre
pre = cur
cur = temp
return pre
这里也补充一下递归的写法,注意if的两个分支都要return,可以看到每一部分都和双指针法是对应的
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
def reverse(cur, pre):
if cur == None:
return pre
else:
temp = cur.next
cur.next = pre
return reverse(temp, cur)
return reverse(head, None)
代码随想录算法训练营第三天 | 203. 移除链表元素、707. 设计链表、206. 反转链表_小蛙先森的博客-CSDN博客