Skip to content

feat: update solutions to lc problems: No.0083,0095 #2167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 76 additions & 24 deletions solution/0000-0099/0083.Remove Duplicates from Sorted List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:一次遍历**

我们用一个指针 $cur$ 来遍历链表。如果当前 $cur$ 与 $cur.next$ 对应的元素相同,我们就将 $cur$ 的 $next$ 指针指向 $cur$ 的下下个节点。否则,说明链表中 $cur$ 对应的元素是不重复的,因此可以将 $cur$ 指针移动到下一个节点。

遍历结束后,返回链表的头节点即可。

时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

### **Python3**
Expand All @@ -61,25 +69,6 @@ class Solution:
return head
```

```python
# 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]:
dummy = ListNode(1000)
cur = dummy
while head:
if head.val != cur.val:
cur.next = head
cur = cur.next
head = head.next
cur.next = None
return dummy.next
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->
Expand Down Expand Up @@ -142,19 +131,53 @@ public:
### **Go**

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
current := head
for current != nil && current.Next != nil {
if current.Val == current.Next.Val {
current.Next = current.Next.Next
cur := head
for cur != nil && cur.Next != nil {
if cur.Val == cur.Next.Val {
cur.Next = cur.Next.Next
} else {
current = current.Next
cur = cur.Next
}
}
return head
}
```

### **JavaScript**

```js
/**
* 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;
};
```

### **Rust**

```rust
Expand Down Expand Up @@ -192,6 +215,35 @@ impl Solution {
}
```

### **C#**

```cs
/**
* 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;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@

## Solutions

**Solution 1: Single Pass**

We use a pointer $cur$ to traverse the linked list. If the element corresponding to the current $cur$ is the same as the element corresponding to $cur.next$, we set the $next$ pointer of $cur$ to point to the next node of $cur.next$. Otherwise, it means that the element corresponding to $cur$ in the linked list is not duplicated, so we can move the $cur$ pointer to the next node.

After the traversal ends, return the head node of the linked list.

The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -53,25 +61,6 @@ class Solution:
return head
```

```python
# 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]:
dummy = ListNode(1000)
cur = dummy
while head:
if head.val != cur.val:
cur.next = head
cur = cur.next
head = head.next
cur.next = None
return dummy.next
```

### **Java**

```java
Expand Down Expand Up @@ -132,19 +121,53 @@ public:
### **Go**

```go
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
current := head
for current != nil && current.Next != nil {
if current.Val == current.Next.Val {
current.Next = current.Next.Next
cur := head
for cur != nil && cur.Next != nil {
if cur.Val == cur.Next.Val {
cur.Next = cur.Next.Next
} else {
current = current.Next
cur = cur.Next
}
}
return head
}
```

### **JavaScript**

```js
/**
* 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;
};
```

### **Rust**

```rust
Expand Down Expand Up @@ -182,6 +205,35 @@ impl Solution {
}
```

### **C#**

```cs
/**
* 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;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
public class Solution {
public ListNode DeleteDuplicates(ListNode head) {
if (head == null) return null;
var last = head;
var current = head.next;
while (current != null)
{
if (current.val != last.val)
{
last.next = current;
last = current;
}
else
{
last.next = null;
}
current = current.next;
}
return head;
}
/**
* 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;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func deleteDuplicates(head *ListNode) *ListNode {
current := head
for current != nil && current.Next != nil {
if current.Val == current.Next.Val {
current.Next = current.Next.Next
cur := head
for cur != nil && cur.Next != nil {
if cur.Val == cur.Next.Val {
cur.Next = cur.Next.Next
} else {
current = current.Next
cur = cur.Next
}
}
return head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
* @return {ListNode}
*/
var deleteDuplicates = function (head) {
var p = head;
while (p && p.next) {
if (p.val == p.next.val) {
p.next = p.next.next;
let cur = head;
while (cur && cur.next) {
if (cur.next.val === cur.val) {
cur.next = cur.next.next;
} else {
p = p.next;
cur = cur.next;
}
}
return head;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,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: ListNode) -> 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
# 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
Loading