203.移除链表元素
题目
题意:删除链表中等于给定值 val 的所有节点。
示例 1:
输入:head = [1,2,6,3,4,5,6], val = 6
输出:[1,2,3,4,5]
思路
删除链表元素的步骤
- 被删除节点的前一个节点指向被删除节点的下一个节点
代码
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)
curr =dummy_head
while curr.next != None:
if curr.next.val == val:
curr.next = curr.next.next
else:
curr = curr.next
return dummy_head.next
707.设计链表
题目
在链表类中实现这些功能:
- get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
- addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
- addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
- addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。如果index小于0,则在头部插入节点。
- deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。
思路
注意
- 除了get函数,剩余的方法里cur永远不会指向链表的最后一个节点;
- 添加节点时,需要先操作new node 再操作pre node;
- 删除节点时,注意边界条件,index != self.size,否则在删除最后一个节点时会出现空指针异常的情况;
代码
class Node:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class MyLinkedList:
def __init__(self):
self.dummy_head = Node()
self.size = 0
def get(self, index: int) -> int:
"""
index starts with zero,
return the value of the node at index
"""
# *if not sure about it, assuming index = 0 to see if it returns correct value
cur = self.dummy_head.next
if index < 0 or (index >= self.size):
return -1
for _ in range(index):
cur = cur.next
return cur.val
def addAtHead(self, val: int) -> None:
self.addAtIndex(0, val)
def addAtTail(self, val: int) -> None:
self.addAtIndex(self.size, val)
# or if cur.next is None, then adding a new node at the end
def addAtIndex(self, index: int, val: int) -> None:
"""
the reson why the cur = self.dummy_head is that when we want to add/del a node at index,
we must operate the previous node of the node at index
"""
cur = self.dummy_head
if index > self.size:
return
for _ in range(index):
cur = cur.next
new_node = Node(val, next=cur.next)
cur.next = new_node
self.size += 1
def deleteAtIndex(self, index: int) -> None:
# ! we need to ensure that cur.next is the node we want to delete
cur = self.dummy_head
# the reson why index >= self.size is that when we delete the node, we not only need to operate the previous node,
# but we also need to operate the node itself at index, such as node.next.next
if index < 0 or index >= self.size:
return
for _ in range(index):
cur = cur.next
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.反转链表
题目
思路
代码
#
# @lc app=leetcode.cn id=206 lang=python3
#
# [206] 反转链表
#
# @lc code=start
# 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]:
pre = None
cur = head
while cur:
tmp = cur.next
cur.next = pre
pre = cur
cur = tmp
return pre
# @lc code=end