题目 203.移除链表元素
题目描述:
给定链表头和整数 val,删除链表中所有具有 Node.val == val 的节点,并返回新头。
解题思路:
本题为给定一个值,在链表中找到并删除同等值的节点,在python中,假如链表为:
a-->b-->c, 要删除b节点不能直接用数组里面的remove,链表涉及到一个指向的问题,将a节点直接指向c就行了,但是如果删除的是头结点又该怎么办呢?
这里就涉及链表操作的两种方式:
- 直接使用原来的链表来进行删除操作。
- 设置一个虚拟头结点在进行删除操作。(推荐)
代码:
记住中途利用变量cur来寻找val,return的话,要用dummy_head.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]:
#创造一个虚拟头节点,在虚拟节点不指向null的情况下,找target的值,如果没找到,就继续往后边移动
#create the dummy node
dummy_head=ListNode(next=head)
cur=dummy_head
#while the next of dummy!=none, the code will be processed
while(cur.next!=None):
if cur.next.val==val:
cur.next=cur.next.next
else:
cur=cur.next
return dummy_head.next
复杂度:
时间复杂度:
在这个问题中,我们只需要遍历链表一次,对于每个节点,我们只做常数级别的操作(比如比较和修改指针),所以时间复杂度是O(n),其中n是链表的长度。
空间复杂度:
在这个问题中,我们只使用了固定数量的额外空间(比如dummy_head和cur),不管链表的长度是多少,所以空间复杂度是O(1)。
题目 707.设计链表
题目描述:
设计链表的实现。 您可以选择使用单链表或双向链表。
单向链表中的节点应该有两个属性:val 和 next。 val 是当前节点的值,next 是指向下一个节点的指针/引用。
如果要使用双向链表,还需要一个属性prev来表示链表中的前一个节点。 假设链表中的所有节点都是从 0 开始索引的。
实现 MyLinkedList 类:
MyLinkedList() 初始化 MyLinkedList 对象。
int get(int index) 获取链表中第index个节点的值。 如果索引无效,则返回 -1。
void addAtHead(int val) 在链表的第一个元素之前添加一个值为 val 的节点。 插入后,新节点将成为链表的第一个节点。
void addAtTail(int val) 添加一个值为 val 的节点作为链表的最后一个元素。
void addAtIndex(int index, int val) 在链表的第index个节点前添加一个值为val的节点。 如果索引等于链表的长度,则该节点将追加到链表的末尾。 如果索引大于长度,则不会插入该节点。
void deleteAtIndex(int index) 如果索引有效,则删除链表中的第 index 个节点。
example:
Input ["MyLinkedList", "addAtHead", "addAtTail", "addAtIndex", "get", "deleteAtIndex", "get"] [[], [1], [3], [1, 2], [1], [1], [1]] Output [null, null, null, null, 2, null, 3] Explanation MyLinkedList myLinkedList = new MyLinkedList(); myLinkedList.addAtHead(1); myLinkedList.addAtTail(3); myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3 myLinkedList.get(1); // return 2 myLinkedList.deleteAtIndex(1); // now the linked list is 1->3 myLinkedList.get(1); // return 3
解题思路:
MyLinkedList
类的初始化方法__init__
会创建一个空链表,head
节点的val
和next
属性都是None
,size
是0。
get
方法会获取指定索引处的值。如果索引无效(小于0或大于等于链表的大小),则返回-1。否则,从头节点开始,沿着next
指针向后走,直到到达指定的索引,然后返回那个节点的值。
addAtHead
方法会在链表头部添加一个新的节点。首先,创建一个新的节点,然后将新节点的next
指针指向当前的头节点的next
节点,然后将头节点的next
指针指向新节点。
addAtTail
方法会在链表尾部添加一个新的节点。首先,创建一个新的节点,然后从头节点开始,沿着next
指针向后走,直到到达最后一个节点,然后将最后一个节点的next
指针指向新节点。
addAtIndex
方法会在链表指定索引处添加一个新的节点。首先,检查索引的有效性,然后从头节点开始,沿着next
指针向后走,直到到达指定的索引,然后插入新的节点。
deleteAtIndex
方法会删除链表指定索引处的节点。首先,检查索引的有效性,然后从头节点开始,沿着next
指针向后走,直到到达指定索引的前一个节点,然后更改那个节点的next
指针,使其指向被删除节点的next
节点。
代码:
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.head = Node()
self.size = 0
def get(self, index: int) -> int:
if index < 0 or index >= self.size:
return -1
cur = self.head.next
for _ in range(index):
cur = cur.next
return cur.val
def addAtHead(self, val: int) -> None:
new_node = Node(val)
new_node.next = self.head.next
self.head.next = new_node
self.size += 1
def addAtTail(self, val: int) -> None:
new_node = Node(val)
if self.size == 0:
self.head.next = new_node
else:
cur = self.head.next
while cur.next:
cur = cur.next
cur.next = new_node
self.size += 1
def addAtIndex(self, index: int, val: int) -> None:
if index < 0:
self.addAtHead(val)
return
elif index == self.size:
self.addAtTail(val)
return
elif index > self.size:
return
new_node = Node(val)
cur = self.head
for _ in range(index):
cur = cur.next
new_node.next = cur.next
cur.next = new_node
self.size += 1
def deleteAtIndex(self, index: int) -> None:
if index < 0 or index >= self.size:
return
pre = self.head
for _ in range(index):
pre = pre.next
pre.next = pre.next.next
self.size -= 1
复杂度:
get
方法、addAtIndex
方法和deleteAtIndex
方法的时间复杂度为O(n),其中n是链表的长度。这是因为在最坏的情况下,我们需要遍历整个链表才能找到目标节点。addAtHead
方法和addAtTail
方法的时间复杂度为O(1),因为我们可以直接找到需要操作的节点。- 对于所有操作,空间复杂度为 O(1),因为我们仅使用了有限的额外空间(例如新节点和临时变量)。
题目 206.反转链表
题目描述:
Given the head
of a singly linked list, reverse the list, and return the reversed list.
解题思路:
这道题是总体链表的一个反转,我们把问题简化,就是两两node之间变换指向:
1---> 2---> 3-->None
让头节点 1 指向None: None(pre)<---1(cur)---> 2--->3-->None
让节点2指向节点1: None<---1(pre)<--- 2(cur)--->3-->None
让节点3指向节点2: None<---1<--- 2(pre)<---3(cur)
设置两个指针分别为, pre和cur,然后移动变换方向
注意,提前用temp保存cur的下一个节点,以便后续变换指向操作
代码:
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
pre=None
cur=head
while(cur!=None):
temp=cur.next
cur.next=pre
pre=cur
cur=temp
return pre
为什么不return cur呢,由while条件可以知道,最后cur的值为none,none不指向任何数,只有pre作为head了,所以return pre就行
复杂度:
- 时间复杂度:O(n),其中 n 是链表的长度。我们需要遍历每个节点一次来反转
next
指针。 - 空间复杂度:O(1),我们只使用了两个指针,所以空间复杂度是常数级别的。