链表
题目描述
给定一个已排序的链表的头 head
, 删除所有重复的元素,使每个元素只出现一次 。返回 已排序的链表 。
示例 1:
输入: head = [1,1,2]
输出: [1,2]
示例 2:
输入: head = [1,1,2,3,3]
输出: [1,2,3]
提示:
链表中节点数目在范围 [0, 300]
内
-100 <= Node.val <= 100
题目数据保证链表已经按升序 排列
解法
方法一:一次遍历
我们用一个指针 $cur$ 来遍历链表。如果当前 $cur$ 与 $cur.next$ 对应的元素相同,我们就将 $cur$ 的 $next$ 指针指向 $cur$ 的下下个节点。否则,说明链表中 $cur$ 对应的元素是不重复的,因此可以将 $cur$ 指针移动到下一个节点。
遍历结束后,返回链表的头节点即可。
时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。
Python3 Java C++ Go Rust JavaScript C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14 # Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution :
def deleteDuplicates ( self , head : Optional [ ListNode ]) -> Optional [ ListNode ]:
cur = head
while cur and cur . next :
if cur . val == cur . next . val :
cur . next = cur . next . next
else :
cur = cur . next
return head
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode deleteDuplicates ( ListNode head ) {
ListNode cur = head ;
while ( cur != null && cur . next != null ) {
if ( cur . val == cur . next . val ) {
cur . next = cur . next . next ;
} else {
cur = cur . next ;
}
}
return head ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public :
ListNode * deleteDuplicates ( ListNode * head ) {
ListNode * cur = head ;
while ( cur != nullptr && cur -> next != nullptr ) {
if ( cur -> val == cur -> next -> val ) {
cur -> next = cur -> next -> next ;
} else {
cur = cur -> next ;
}
}
return head ;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 /**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates ( head * ListNode ) * ListNode {
cur := head
for cur != nil && cur . Next != nil {
if cur . Val == cur . Next . Val {
cur . Next = cur . Next . Next
} else {
cur = cur . Next
}
}
return head
}
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
30
31
32 // 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_duplicates ( head : Option < Box < ListNode >> ) -> Option < Box < ListNode >> {
let mut dummy = Some ( Box :: new ( ListNode :: new ( i32 :: MAX )));
let mut p = & mut dummy ;
let mut current = head ;
while let Some ( mut node ) = current {
current = node . next . take ();
if p . as_mut (). unwrap (). val != node . val {
p . as_mut (). unwrap (). next = Some ( node );
p = & mut p . as_mut (). unwrap (). next ;
}
}
dummy . unwrap (). next
}
}
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
* @return {ListNode}
*/
var deleteDuplicates = function ( head ) {
let cur = head ;
while ( cur && cur . next ) {
if ( cur . next . val === cur . val ) {
cur . next = cur . next . next ;
} else {
cur = cur . next ;
}
}
return head ;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 /**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode DeleteDuplicates ( ListNode head ) {
ListNode cur = head ;
while ( cur != null && cur . next != null ) {
if ( cur . val == cur . next . val ) {
cur . next = cur . next . next ;
} else {
cur = cur . next ;
}
}
return head ;
}
}
GitHub