● 203.移除链表元素
技巧:构建虚拟节点
class Solution:
def removeElements(self, head: Optional[ListNode], val: int) -> Optional[ListNode]:
dummy_head=ListNode(next=head)
current=dummy_head
while current.next:
if current.next.val==val: ##下一节点的值
current.next=current.next.next
else:
current=current.next ##移动链表指针往下
return dummy_head.next
● 707.设计链表
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
else:
current = self.dummy_head.next
while index > 0:
current = current.next
index -= 1
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
else:
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
else:
current = self.dummy_head
for i in range(index):
current = current.next
current.next = current.next.next
self.size -= 1
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
else:
current = self.dummy_head.next
while index > 0:
current = current.next
index -= 1
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:
if self.size == 0:
self.dummy_head.next = ListNode(val)
else:
current = self.dummy_head.next
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
else:
current = self.dummy_head
while index > 0:
current = current.next
index -= 1
current.next = ListNode(val, current.next)
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
else:
current = self.dummy_head
while index > 0:
current = current.next
index -= 1
current.next = current.next.next
self.size -= 1
这道题写的十分糟糕,看了好几次答案都没调试出来,以下是调试不出来的代码
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
else:
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:
if self.size == 0:
self.dummy_head.next = listNode(val)
current=self.dummy_head.next
while current.next:
current=current.next
current.next=listnode(val)
self.size+=1
def addAtIndex(self, index: int, val: int) -> None:
current=self.dummy_head.next
if index<0 or index>=self.size:
return
else:
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
else:
current=self.dummy_head
for i in range(index):
current=current.next
current.next=current.next.next
self.size-=1
● 206.反转链表
方法一:双指针
相对比较简单,但是要注意各变量的赋值顺序,容易乱
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
cur=head
pre=None
while cur!=None:
temp=cur.next
cur.next=pre
pre=cur
cur=temp
return pre
方法二:递归
不太熟练
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
return self.reverse(head,None)
def reverse(self,cur:ListNode,pre:ListNode):
if cur==None:
return pre
else:
temp=cur.next
cur.next=pre
return self.reverse(temp,cur)