# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def deleteNode(self, head, val):
"""
:type head: ListNode
:type val: int
:rtype: ListNode
"""
if head.val == val:
return head.next
pre = head
cur = head.next
while cur and cur.val != val:
pre = cur
cur = cur.next
if cur:
pre.next = cur.next
return head
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val == val:
return head.next
pre = head
cur = head.next
while cur.val != val and cur:
pre = cur
cur = cur.next
if cur.val == val: ## 为什么 可以换成 if cur
pre.next = cur.next
return head
python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
cur = head
pre = None
while cur is not None:
#找到删除的元素
if cur.val== val:
#元素在头部
if cur == head:
head = cur.next
else:
#元素不在头部
pre.next = cur.next
return head
else:
pre = cur
cur = cur.next
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head None:
return None
if head.val val:
return head.next
pre = head
temp = head.next
while temp != None:
if temp.val val:
pre.next = temp.next
return head
else:
pre = temp
temp = temp.next
return head
“`java
var deleteNode = function (head, val) {
//定义头结点和前一个节点
let cur = pre = new ListNode();
cur = head.next;//当前节点
pre = head;
while (cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
if(cur != null) pre.next = cur.next;
return head;
};
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
思路:
先判断特殊情况,一种是链表为空,一种为头结点为待删除节点的情况。之后设置双指针,找到待删除节点进行删除。
时间复杂度:O(N) 空间复杂度:O(1)
”’python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val val: return head.next
pre, cur = head, head.next
while cur and cur.val != val:
pre, cur = cur, cur.next
if cur: pre.next = cur.next
return head
···python
class Solution(object):
def deleteNode(self, head, val):
“””
:type head: ListNode
:type val: int
:rtype: ListNode
“””
if head.val val: return head.next
pre, cur = head, head.next
while cur and cur.val != val:
pre, cur = cur, cur.next
if cur: pre.next = cur.next
return head
提交
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteNode(head *ListNode, val int) *ListNode {
if head.Val == val {
return head.Next
}
pre := head
for pre.Next != nil && pre.Next.Val != val {
pre = pre.Next
}
if pre.Next != nil {
pre.Next = pre.Next.Next
}
return head
}
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if not head:
return head
if head.val == val:
return head.next
pre = head
tmp = head.next
while tmp != None:
if tmp.val == val:
pre.next = tmp.next
return head
pre = pre.next
tmp = tmp.next
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head;
while(cur!=null){
if(cur.val!=val){
pre = cur;
cur = cur.next;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if (head null){
return null;
}
if (head.val val){
return head.next;
}
ListNode a = head.next;
ListNode pre = head;
while(a != null){
if(a.val == val){
pre.next = a.next;
}
a = a.next;
pre = pre.next;
}
return head;
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
head = head
cur = head
next = head.next
if cur.val == val:
head = head.next
return head
while next.val != None:
if next.val == val:
next = next.next
cur.next = next
return head
cur = cur.next
next = next.next
return
打卡
python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(-1)
dummy.next = head
pre = dummy
cur = head
while cur:
if cur.val val:
pre.next = cur.next
return dummy.next
pre = pre.next
cur = cur.next
return dummy.next
“`/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode p = dummy;
while (p.next !=null)
{
if (p.next.val==val)
{
p.next = p.next.next;
}else{
p = p.next;
}
}
return dummy.next;
}
共以下 86 个回答
打卡1
首先判断特殊情况
1.头节点是否为空
2.要删除的是否为头节点
然后进行初始化,当前节点和前节点
之后进行循环,当前节点不为空,进行判断,如果有要删除的值,就删除,没有就接着向下遍历
最后while循环结束,返回头节点
java
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head null){
return null;
}
}
“`c++
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
//删除头节点
while (head != nullptr && head->val ==val) {
ListNode *temp = head;
head = head->next;
//delete temp;
}
}
};
“`
第一次打卡
时间复杂度O(N),空间复杂度O(1)
“`c++
ListNode* deleteNode(ListNode* head, int val) {
ListNode* dummynode=new ListNode(0);
dummynode->next=head;
ListNode* pn=dummynode;
while(pn->next!=NULL){
if(pn->next->val==val){
pn->next=pn->next->next;
continue;
}
pn=pn->next;
}
return dummynode->next;
}
“`C++
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* dummy = new ListNode(0,head);
ListNode* cur = head;
ListNode* pre = dummy;
if(cur == NULL) return head;
while(cur->val != val){
cur = cur->next;
pre = pre->next;
}
pre->next = cur->next;
return dummy->next;
}
};
“`
头节点
python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
cur = head
pre = None
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if (head null){
return null;
}
if (head.val val) {
return head.next;
}
ListNode pre = head;//记录上个节点的位置
ListNode cur = head.next;//要删除的节点
while (cur!=null){
if (cur.val val){//说明找到删除的节点了,需要删除
pre.next = cur.next;
return head;
}
pre = pre.next;
cur = cur.next;
}
return head;
}
}
用两个指针,最后没有查找到的话,直接返回头结点。
时间O(n)空间O(1)
Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head None:
return None
if head.val val:
return head.next
pre = head
temp = head.next
while temp != None:
if temp.val val:
pre.next = temp.next
return head
else:
pre = temp
temp = temp.next
return head
public ListNode deleteNode(ListNode head, int val) {
if (head null) return null;
if (head.val val) return head.next;
ListNode cur = head;
while (cur.next != null && cur.next.val != val)
cur = cur.next;
if (cur.next != null)
cur.next = cur.next.next;
return head;
}
思路:遍历链表,遇到val值就删除
时间O(n)空间O(1)
“`java
var deleteNode = function (head, val) {
//定义头结点和前一个节点
let cur = pre = new ListNode();
cur = head.next;//当前节点
pre = head;
while (cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
if(cur != null) pre.next = cur.next;
return head;
};
打卡
“`java
思路:因为是单向链表,并且知道头结点和需要删除的节点的值,因此从前向后遍历获取节点的值,与需要删除的值进行比较判断,相等就删除,不相等,就继续遍历下一个节点的值,循环处理。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head null){
return null;
}
if(head.val val){
return head.next;
}else{
head.next = deleteNode(head.next,val);
}
return head;
}
}
时间复杂度O(n):删除操作平均需进行N/2次,最多N次
空间复杂度O(1):head,val只占常数大小空间,没有额外的空间占用。
”’C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode deleteNode(ListNode* head, int val) {
if(head NULL){
return NULL;
}
};
时间复杂度O(n),空间复杂度O(1)
class Solution
{
// 删除链表元素
public:
ListNode* removeElements(ListNode* head, int val)
{
ListNode* dummyHead = new ListNode(0); // 实例化dummyHead
dummyHead->next = head; // 虚拟头结点指向头节点
ListNode* cur = dummyHead;
while (cur->next != NULL)
{
if(cur->next->val val)
{
ListNode* temp = cur->next; //指针temp指向cur->next地址
cur->next = cur->next->next;//
delete temp;
}
else
{
cur = cur->next;
}
};
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head null) {
return head;
}
if(head.val val) {
return head.next;
}
ListNode pre = head;
ListNode cur = head.next;
while(cur != null) {
if(cur.val val) {
pre.next = cur.next;
}
pre = cur;
cur = cur.next;
}
return head;
}
}
时间复杂度O(n),空间复杂度O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val val) {
return head.next;
}
ListNode pre = head,cur = head.next;
while(cur != null){
if(cur.val val){
pre.next = cur.next;
cur.next = null;
}
pre = cur;
cur = cur.next;
}
return head;
}
}
时间复杂度O(n),空间复杂度O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
}
时间复杂度O(n),空间复杂度O(1)。
时间复杂度O(n),空间复杂度o(1)
基础的链表题,依据题给结构解答即可,不让忘记判断头节点是否为空
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode deleteNode(ListNode* head, int val) {
if(headnullptr)
{
return nullptr;
}
if(head->valval)
{
return head->next;
}
ListNode *temp = head->next;
ListNode *pre = head;
while(temp!= nullptr)
{
if(temp->valval)
{
pre->next = temp->next;
return head;
}
temp = temp->next;
pre = pre->next;
}
return head;
}
};
用pre指针遍历链表,当遇到pre下一个节点元素为所删除的值时,将pre->next指向pre->next->next,实现删除操作
对头节点为所删除值得情况,直接返回head->next
时间复杂度为O(n),空间复杂度为O(1)
思路:
先判断特殊情况,一种是链表为空,一种为头结点为待删除节点的情况。之后设置双指针,找到待删除节点进行删除。
时间复杂度:O(N) 空间复杂度:O(1)
”’python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
if head.val val: return head.next
pre, cur = head, head.next
while cur and cur.val != val:
pre, cur = cur, cur.next
if cur: pre.next = cur.next
return head
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head->val val) return head->next;
ListNode *pre = head, *cur = head->next;
while(cur != nullptr && cur->val != val){
pre = cur;
cur = cur->next;
}
if(cur != nullptr){
pre->next = cur->next;
}
return head;
}
};
时间复杂度:O(n)
空间复杂度:O(1)
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head null)
return null;
时间复杂度:O(n)
空间复杂度:O(1)
Definition for singly-linked list.
class ListNode(object):
def init(self, x):
self.val = x
self.next = None
class Solution(object):
def deleteNode(self, node):
“””
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
“””
node.val = node.next.val
node.next = node.next.next
时间time: O(1)
空间space: O(1)
java
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val val) return head.next;
ListNode pre = head, cur = head.next;
while(cur != null && cur.val != val){
pre = cur;
cur = cur.next;
}
if(cur != null)pre.next = cur.next;
return head;
}
}
提交
···python
class Solution(object):
def deleteNode(self, head, val):
“””
:type head: ListNode
:type val: int
:rtype: ListNode
“””
if head.val val: return head.next
pre, cur = head, head.next
while cur and cur.val != val:
pre, cur = cur, cur.next
if cur: pre.next = cur.next
return head
提交
”’C++
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if (head NULL) return head;
if(head->val val) {
head = head->next;
return head;
}
ListNode* slow = head;
ListNode* fast = head->next;
while(fast!= NULL) {
if(fast->val val) {
slow->next = fast->next;
break;
}
fast = fast->next;
slow = slow->next;
}
return head;
}
};
思路:
思路:
考虑是否是删除头结点
如果是直接返回head.next
否则
用pre和cur分别表示第一个第二个结点,检查cur是否为None 且cur.val是否是我们想要删除的结点值,一直循环,知道cur表示是我们要删除的结点
然后让pre.next=cur.next此时已经完成删除操作
返回head就可以
时间复杂度:o(n)
空间复杂度:o(1)
代码:
public ListNode deleteNode(ListNode head, int val) {
if(head null){
return null;
}
思路:双指针循环遍历,复杂度:O(n)
代码:
“`java
public ListNode deleteNode(ListNode head, int val) {
if(head==null){
return head;
}
if(val==head.val){
return head.next;
}
ListNode pre=null;
ListNode cur=head;
while(null!=cur){
if(val==cur.val) {
pre.next=cur.next;
break;
}
pre=cur;
cur=cur.next;
}
return head;
}
时间复杂度 O(N)
空间复杂度O(1)
”’java
代码
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.valval){
return head.next;
}
ListNode pre=new ListNode();
ListNode p=head;
}
遍历列表,如果为空直接返回空,如果头节点为想要删除的节点则直接返回头节点的下一个节点。其他情况就用两个指针遍历,如果后一个指针指向想要删除的节点,则前一个指针直接指向该节点的下一个节点,对其进行删除。
空间和时间都是O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head null){
return null;
}
if(head.val val){
head = head.next;
return head;
}
ListNode pre = head, cur = head.next;
while(cur != null &&cur.val != val){
pre = cur;
cur = cur.next;
}
if (cur != null){
pre.next = cur.next;
}
return head;
}
}
时间复杂度 O(n)
空间复杂度 O(1)
时间复杂度 O(n)
空间复杂度 O(1)
时间复杂度 O(n)
空间复杂度 O(1)
时间复杂度 O(n)
空间复杂度 O(1)
设置pre和tmp两个指针:
pre方便处理删除节点的操作,
tmp的作用则是寻找目标节点。
tmp找到目标节点以后,把pre的next指针指向tmp的下一个节点,完成删除操作,并返回头结点。
如果没有找到则继续移动pre和tmp。
时间复杂度O(n):最坏情况目标节点在尾节点,要遍历一遍链表。
空间复杂度O(1):两个指针占用常量空间。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode virHead=new ListNode(-1);
virHead.next=head;
ListNode node=virHead;
while(node!=null)
{
if((node.next!=null)&&(node.next.valval))
{
ListNode temp=node.next;
node.next=temp.next;
node=temp.next;
}
else
node=node.next;
}
return virHead.next;
}
}
利用虚拟节点方便删除指定值的链表
时间复杂度 O(N)
空间复杂度 O(1)
“`C++
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
//如果头节点指向的是目标值,直接返回原头节点的下一个节点
if(head->val==val){
return head->next;
}
//若头节点指向的不是目标值,利用双指针遍历
ListNode *slow=head;
ListNode *fast=head;
while(fast!=nullptr){
if(fast->val==val){
slow->next=fast->next;
break;
}
else{
slow=fast;
fast=fast->next;
}
}
return head;
}
};
//时间复杂度O(n)
//空间复杂度O(1)
“`
一般的数据结构教科书上都有图解,注意pre.next = cur.next,这是指让pre节点的下一个直接指向cur节点的下一个,随后把cur和链表断连,余下的cur节点等待gc吃掉就是
class Solution {
public ListNode deleteNode(ListNode head, int val) {
// 处理头节点的情况
if (head != null && head.val val) {
return head.next;
}
}
时间复杂度 O(N)
空间复杂度 O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head;
while(cur!=null){
if(cur.val!=val){
pre = cur;
cur = cur.next;
}
时间复杂度 O(N)
空间复杂度 O(1)
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(headnullptr)return nullptr;
};
时间复杂度 O(N)
空间复杂度 O(1)
时间复杂度O(N + M)空间复杂度)(1)
“`python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
pre = ListNode()
pre.next = head
head = pre
虚拟头节点法
时间:O(N)空间O(1)
struct ListNode* deleteNode(struct ListNode* head, int val){
if(NULL head){
return NULL;
}
}
时间复杂度:O(n);空间复杂度O(1)
我的代码和地哥给的是一样的。我觉得这题难就难在有一些边边的那种你没有考虑到,就比如说假如这个链表是空链表怎么办?假如这个链表第一个就是要删除的元素怎么办?就这两点有点难考虑周全。
题目的思路是:
1.先判断这个链表是不是空链表
2.假如第一个元素就是要删除的元素怎么办
3.常规的删除链表元素
public ListNode deleteNode(ListNode head, int val) {
if(head null){
return null;
}
总结:我感觉这道题也用了双指针,用双指针来配合删除链表中的元素,这个思路其实挺好的。
“`java
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode res = head;
if (res.val val)
return res.next;
ListNode cur = head;
ListNode pre = new ListNode(0);
pre.next = cur;
while (cur != null) {
if (cur.val val) {
pre.next = cur.next;
cur.next = null;
break;
} else {
pre = cur;
cur = cur.next;
}
}
return res;
}
}
“`java
时间复杂度O(N),空间复杂度O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if (head null){
return null;
}
if (head.val val){
return head.next;
}
}
首先判断该链表是否为空;然后要分为两种情况来讨论,对于头结点,直接判断;非头结点,需要新增一个中间结点,对于值相同的结点进行删除。
时间复杂度为O(n)
空间复杂度为O(1)
时间复杂度O(n)
空间复杂度O(1)
Definition for singly-linked list.
class ListNode:
def init(self, x):
self.val = x
self.next = None
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
head = head
cur = head
next = head.next
straightforward
time时间:O(n) space空间:O(1)
删除链表节点
判断异常情况后,逐个遍历去判断是否为删除的节点。
时间复杂度:O(n);
空间复杂度:O(1)。
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head null ){
return null;
}
if(head.val val){
head= head.next;
}
ListNode pre = head;
ListNode now = head.next;
while(now != null && now.val != val){
pre = now;
now = now.next;
}
if(now != null)
pre.next = now.next;
return head;
}
}
时间复杂 O(N) 空间复杂O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
// 特殊处理
if(head null) {
return null;
}
if(head.val val) {
return head.next;
}
}
先特判,然后应用双指针,保存cur的前节点用作删除
时间复杂度O(n),空间复杂度O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
//预处理
if(head null){
return null;
}
//虚拟头节点
ListNode dumyNode = new ListNode(-1);
dumyNode.next = head;
//快慢指针
ListNode fastNode = dumyNode.next;
ListNode slowNode = dumyNode;
//当快指针跟要删除的值相等,退出循环,让慢指针直接指向快指针的下一个节点
while(fastNode.val != val){
fastNode = fastNode.next;
slowNode = slowNode.next;
}
fastNode = fastNode.next;
slowNode.next = fastNode;
return dumyNode.next;
}
}
“` C++
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if(head->val == val) return head->next;
ListNode *pre = head, *cur = head->next;
while(cur != nullptr && cur->val != val)
{
pre = cur;
cur = cur->next;
}
if(cur != nullptr) pre->next = cur->next;
return head;
}
};
“`
方法:双指针
时间复杂度:o(n)
空间复杂度:o(1)
如有问题,欢迎指正!
时间复杂度
此代码段的时间复杂度为 O(n),其中 n 是链表的长度。
这是因为我们需要遍历链表来查找要删除的节点。
在最坏的情况下,我们可能需要遍历整个链表才能找到要删除的节点。
空间复杂度
此代码段的空间复杂度为 O(1),
因为我们只使用了有限的额外空间来存储几个指针(preNode 和 currentNode),
这些空间与链表的长度无关,所以是常数空间复杂度。
说明
这段代码的目的是删除链表中的一个特定值的节点。代码首先检查链表是否为空,
如果为空,则直接返回 null。然后,它检查头节点是否是要删除的节点,如果是,则更新头节点为下一个节点。
如果头节点不是要删除的节点,代码会遍历链表来查找要删除的节点。
在遍历过程中,它维护了两个指针:preNode(指向前一个节点)和 currentNode(指向当前节点)。
当 currentNode 的值等于要删除的值时,代码会删除该节点(通过更新 preNode.next 指向 currentNode.next)。
如果链表中没有要删除的节点,则函数返回原始的头节点。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode deleteNode(ListNode* head, int val) {
while(head != NULL && head->valval){
head = head->next;
}
ListNode* cur=head;
while(cur->next != NULL && cur != NULL){
if(cur->next->val val){
cur->next = cur->next->next;
}
else{
cur=cur->next;
}
}
return head;
}
};
时间复杂度: O(n)
空间复杂度: O(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode deleteNode(ListNode* head, int val) {
ListNode* newHead = new ListNode();
newHead->next = head;
ListNode* cur = newHead;
while(cur->next != NULL){
if(cur->next->val val){
cur->next = cur->next->next;
}else{
cur=cur->next;
}
}
head = newHead->next;
return head;
};
思路:
定义一个虚拟头节点,该节点指向给定的头节点,然后依次判断节点值是否等于给定值val,如果相同,则删除该节点:
“`C++
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if (head == NULL) return NULL;
ListNode *prehead = new ListNode(0);
prehead->next = head;
ListNode *cur = prehead;
while (cur->next) {
if (cur->next->val == val) {
cur->next = cur->next->next;
return prehead->next;
}
cur = cur->next;
}
return prehead->next;
}
};
“`
时间复杂度:O(n)
空间复杂度:O(1)
关于时空复杂度,如果有错误的,希望大家能帮忙指出来,谢谢!
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
ListNode* p=head;
while(p!=nullptr)
{
if(p->valval) break;
p=p->next;
}
if(p->nextnullptr)
{
ListNode* q=head;
while(q->next!=p) q=q->next;
q->next=nullptr;
}
else
{
p->val=p->next->val;
p->next=p->next->next;
}
return head;
}
};
时间复杂度O(n),空间复杂度O(1)
class Solution {
public ListNode deleteNode(ListNode head, int val) {
}
打卡
python
class Solution:
def deleteNode(self, head: ListNode, val: int) -> ListNode:
dummy = ListNode(-1)
dummy.next = head
pre = dummy
cur = head
while cur:
if cur.val val:
pre.next = cur.next
return dummy.next
pre = pre.next
cur = cur.next
return dummy.next
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(headnull){
return head;
}
if(head.valval){
return head.next;
}
ListNode temp = new ListNode(0);
temp.next = head;
while(temp.next!=null){
if(temp.next.valval){
temp.next = temp.next.next;
return head;
}
temp = temp.next;
}
return null;
}
时间复杂度:O(n),空间复杂度:O(1)
思路:遍历链表并对值进行比较,找到需要删除的节点。为了容易处理且放置空指针异常,所以加了一个头节点,每次只需要判断temp(头节点)的next是否为null就可以了,然后一直向下遍历
“`/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode dummy = new ListNode();
dummy.next = head;
ListNode p = dummy;
}
“`时间复杂度O(n),空间复杂度O(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode deleteNode(ListNode* head, int val) {
if(head nullptr) return head;
if(head->val val) return head->next;
ListNode *tmp = head;
while(tmp != nullptr){
if(tmp->next->val val){
tmp->next = tmp->next->next;
return head;
}
tmp = tmp->next;
}
return head;
}
};
时间复杂度O(n),空间复杂度O(1)
#c++
”’
class Solution {
public:
ListNode* deleteNode(ListNode* head, int val) {
if ( head nullptr )
{
return nullptr;
}
};
”’
//p指针指向删除结点,q指向p的前一个结点,创建头结点,遍历链表
struct ListNode* deleteNode(struct ListNode* head, int val){
typedef struct ListNode ListNode;
ListNode *headNode = malloc(sizeof(ListNode));
headNode->next = head;
ListNode *p = head;
ListNode *q = headNode;
while(p!=NULL){
if(p->val != val){
p = p->next;
q = q->next;
}else{
q->next = p->next;
p = q->next;
}
}
return headNode->next;
}
时间复杂度 O(n),空间复杂度 O(1)
思路:
1. 若头指针为要删除的结点就直接返回,
2. 从头遍历直至找到要被删除的结点
时间复杂度o(n) 空间复杂度o(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode deleteNode(ListNode* head, int val) {
if(head nullptr){
return nullptr;
}
if(head->val val){
return head->next;
}
ListNode* tmp = head;
ListNode* ret;
for(ListNode* i = tmp->next; i!=nullptr; i = i->next){
if(i->val val){
tmp->next = i->next;
ret = head;
break;
}else tmp = i;
}
return ret;
}
};
先解决空指针和头节点两个特殊情况,接着for循环遍历,找到后break跳出返回
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode node = head;
if(node.val val)
return node.next;
while(node.next != null){
if(node.next.val val){
if(node.next.next!=null)
node.next = node.next.next;
else
node.next = null;
return head;
return head;
}
}
单指针
时间复杂度On
空间复杂度O1
解题思路
考虑删除的点为头结点和尾结点的特殊情况,使用虚拟头结点。
犯错
忘记在所有的删除行为完成后跳出循环,导致超时。
代码
时间复杂度
O(n)
空间复杂度
O(1)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
ListNode newHead = new ListNode(0);
newHead.next = head;
ListNode node = head;
ListNode pre = newHead;
while(node != null ){
if(node.val val) break;
pre = node;
node = node.next;
}
}
复杂度O(n)
时间复杂度O(n),空间复杂度O(1)
}
删除后就可以返回
创建头节点来辅助删除结点 LinkList* dummyhead = new LinkList(0,head);
时间复杂度:O(n)
空间复杂度:O(1)
复杂度O(n)
下面是代码