203.移除链表元素
给你一个链表的头节点 head
和一个整数 val
,请你删除链表中所有满足 Node.val == val
的节点,并返回 新的头节点 。
解题思路:要删除链表,初始思路是将要删除节点的前一个节点与其后一个节点直接连接,相当于中间直接跳过要删除的节点,如图所示:(来源:代码随想录 (programmercarl.com))
因此我们希望在推进链表的结点时要有一定的前瞻性,设置一个虚拟节点可以满足此条件。假设虚拟节点为dummy,设置dummy.next = head,当dummy.next exists,不断向下循环,直到检测出下一个节点为要删除的节点val,接下来直接将当前所在节点与下下一个节点相连即可。
代码如下:
# 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 = ListNode(0)
dummy.next = head #connect dummy to the first node
cur = dummy #assign an indicator
while cur.next:
if cur.next.val != val:
cur = cur.next#move to the next node
else:
cur.next = cur.next.next#delete the mid node
return dummy.next#return the new linked list starting from the next node after dummy
注意在遍历时不能直接使用dummy进行遍历,需要另设一个indicator,如cur进行遍历,dummy.next 用做输出。
时间复杂度: O(n)
空间复杂度: O(1)
707.设计链表
在链表类中实现这些功能:
- get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
- 思路:设置虚拟节点self.dummy_head,再设置一个cur=self.dummy_head.next进行while loop遍历,当cur.next存在且遍历次数i小于index,cur = cur.next,并得到当前val
- 重点注意边界调节设置:if index<0 or index>=self.size
- addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
- 思路:在self.dummy_head后添加一个新节点,再将新节点与原来self.dummy_head.next连接
- addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
- 思路:遍历到最后一个node,直接在最后添加即可
- addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
- 思路:for loop or while loop遍历到要添加节点的前一个index,将钱一个结点的下一个设置为新添加的节点,新添加的节点下一个设置为原来的下一个节点
- deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
- 思路: for loop or while loop 遍历到上一个index节点,直接与下下一个节点连接
代码如下:
class ListNode:
def __init__(self, val,next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.size = 0
self.dummy_head = ListNode(0)
def get(self, index: int) -> int:
if index<0 or index>=self.size:
return -1
cur = self.dummy_head.next
# print(cur.next.val)
i = 0
while i < index:
cur = cur.next
i += 1
return cur.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:
add_node = ListNode(val)
cur = self.dummy_head
while cur.next:
cur = cur.next
cur.next = add_node
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index <0 or index>self.size:
return
cur = self.dummy_head
# print(cur.next.val)
for _ in range(index):
cur = cur.next
new_node = ListNode(val)
next_node = cur.next#store the original next node after the cur node
cur.next = new_node
new_node.next = next_node
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
cur = self.dummy_head
i = 0
while i < index:
cur = cur.next
i += 1
cur.next = cur.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)
206.反转链表
给你单链表的头节点 head
,请你反转链表,并返回反转后的链表。
解题思路:设置双指针pre,cur指代前一个与后一个node,初始状态下pre指代null,cur指代head,先存储cur_next=cur.next,再将cur.next指针指向pre,然后pre=cur,cur=cur_next,不断循环直到pre.next = null
# 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]:
#init pre and cur
pre = None
cur = head
while cur:
# print(cur.val)
cur_next = cur.next
cur.next = pre
pre = cur
cur = cur_next
return pre
注意:不能设置pre为dummy=ListNode(0,head),会造成无限循环。pre一开始设置成null是因为反转后最后一个node指向null