题目描述
给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点。
返回删除后的链表的头节点。
注意: 此题对比原题有改动
示例 1:
输入: head = [4,5,1,9], val = 5
输出: [4,1,9]
解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
示例 2:
输入: head = [4,5,1,9], val = 1
输出: [4,5,9]
解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
说明:
题目保证链表中节点的值互不相同
若使用 C 或 C++ 语言,你不需要 free
或 delete
被删除的节点
解法
方法一:模拟
我们先创建一个虚拟头节点 dummy
,令 dummy.next = head
,然后创建一个指针 cur
指向 dummy
。
遍历链表,当 cur.next.val == val
时,将 cur.next
指向 cur.next.next
,然后跳出循环。
最后返回 dummy.next
即可。
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。
Python3 Java C++ Go Rust JavaScript C# Swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14 # 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 :
dummy = cur = ListNode ( 0 , head )
while cur . next :
if cur . next . val == val :
cur . next = cur . next . next
break
cur = cur . next
return dummy . next
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 /**
* 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 ( 0 , head );
for ( ListNode cur = dummy ; cur . next != null ; cur = cur . next ) {
if ( cur . next . val == val ) {
cur . next = cur . next . next ;
break ;
}
}
return dummy . next ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 /**
* 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 * dummy = new ListNode ( 0 , head );
for ( ListNode * cur = dummy ; cur -> next ; cur = cur -> next ) {
if ( cur -> next -> val == val ) {
cur -> next = cur -> next -> next ;
break ;
}
}
return dummy -> next ;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteNode ( head * ListNode , val int ) * ListNode {
dummy := & ListNode { 0 , head }
for cur := dummy ; cur . Next != nil ; cur = cur . Next {
if cur . Next . Val == val {
cur . Next = cur . Next . Next
break
}
}
return dummy . Next
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 // Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn delete_node ( mut head : Option < Box < ListNode >> , val : i32 ) -> Option < Box < ListNode >> {
let mut cur = & mut head ;
while let Some ( node ) = cur {
if node . val == val {
* cur = node . next . take ();
break ;
}
cur = & mut cur . as_mut (). unwrap (). next ;
}
head
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 /**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var deleteNode = function ( head , val ) {
const dummy = new ListNode ( 0 , head );
for ( let cur = dummy ; cur . next ; cur = cur . next ) {
if ( cur . next . val == val ) {
cur . next = cur . next . next ;
break ;
}
}
return dummy . next ;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 /**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode DeleteNode ( ListNode head , int val ) {
ListNode dummy = new ListNode ( 0 , head );
for ( ListNode cur = dummy ; cur . next != null ; cur = cur . next ) {
if ( cur . next . val == val ) {
cur . next = cur . next . next ;
break ;
}
}
return dummy . next ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 /**
* Definition for singly-linked list.
* public class ListNode {
* var val: Int
* var next: ListNode?
* init(_ val: Int, _ next: ListNode? = nil) {
* self.val = val
* self.next = next
* }
* }
*/
class Solution {
func deleteNode ( _ head : ListNode ?, _ val : Int ) -> ListNode ? {
let dummy = ListNode ( 0 , head )
var current : ListNode ? = dummy
while current ?. next != nil {
if current ?. next ?. val == val {
current ?. next = current ?. next ?. next
break
}
current = current ?. next
}
return dummy . next
}
}