mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2026-03-11 04:14:41 +00:00
链表专题更新cpp代码
This commit is contained in:
@@ -39,6 +39,10 @@
|
||||
|
||||

|
||||
|
||||
**题目代码**
|
||||
|
||||
Java Code:
|
||||
|
||||
```java
|
||||
class Solution {
|
||||
public ListNode deleteDuplicates(ListNode head) {
|
||||
@@ -68,3 +72,35 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C++ Code:
|
||||
|
||||
```cpp
|
||||
class Solution {
|
||||
public:
|
||||
ListNode* deleteDuplicates(ListNode* head) {
|
||||
if(head == nullptr || head->next == nullptr){
|
||||
return head;
|
||||
}
|
||||
ListNode * pre = head;
|
||||
ListNode * low = new ListNode(0);
|
||||
low->next = pre;
|
||||
ListNode * ret = new ListNode(-1);
|
||||
ret = low;
|
||||
while(pre != nullptr && pre->next != nullptr) {
|
||||
if (pre->val == pre->next->val) {
|
||||
while (pre != nullptr && pre->next != nullptr && pre->val == pre->next->val) {
|
||||
pre = pre->next;
|
||||
}
|
||||
pre = pre->next;
|
||||
low->next = pre;
|
||||
}
|
||||
else{
|
||||
pre = pre->next;
|
||||
low = low->next;
|
||||
}
|
||||
}
|
||||
return ret->next;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Reference in New Issue
Block a user