LeetCode-M-Remove Duplicates from Sorted List II
来源:互联网 发布:淘宝全网举报中心网址 编辑:程序博客网 时间:2024/05/21 10:39
题意
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
Subscribe to see which companies asked this question.
解法
实现
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* deleteDuplicates(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode* pre = head; ListNode* cur = head->next; ListNode* next = NULL; while(cur != NULL){ if(pre->val != cur->val){ if(next == NULL){ head = pre; next = pre; }else{ next->next = pre; next = pre; } pre = cur; cur = cur->next; }else{ while(cur != NULL && cur->val == pre->val) cur = cur->next; if(cur == NULL && next == NULL) return NULL; else if(cur == NULL && next != NULL){ next->next = NULL; return head; }else if(cur != NULL && next == NULL){ pre = cur; cur = cur->next; if(cur == NULL) return pre; }else{ next->next = cur; pre = cur; cur = cur->next; } } } return head; }};
0 0
- LeetCode(M) Remove Duplicates from Sorted List II
- LeetCode-M-Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- LeetCode: Remove Duplicates from Sorted List II
- [Leetcode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode] Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- [LeetCode]Remove Duplicates from Sorted List II
- [leetcode]Remove Duplicates from Sorted List II
- LeetCode-Remove Duplicates from Sorted List II
- [leetcode] Remove Duplicates from Sorted List II
- LeetCode - Remove Duplicates from Sorted List II
- LeetCode | Remove Duplicates from Sorted List II
- LeetCode:Remove Duplicates from Sorted List II
- 【leetcode】Remove Duplicates from Sorted List II
- Leetcode: Remove Duplicates from Sorted List II
- 在 Word 中撰写并发布到博客的帮助
- windows ffmpeg mingw编译流程及vs中的使用方法(vs2005及其以上可用)
- 克隆二叉树
- 这个博客就不更新了 本人做了个社区欢迎大家过来捧场
- 4817: [Sdoi2017]树点涂色
- LeetCode-M-Remove Duplicates from Sorted List II
- Statement.execute() 返回值
- 启动3个线程,线程1打印1到5,线程2打印5到10,线程3打印11到15,然后线程1打印16到20,以此类推。。。打印到30为止
- python学习之编码小技巧
- 项目零散知识积累(一)
- HTML常用标签
- String类和StringBuffer类的区别
- npm install node-sass fail
- 树形结合 斜率优化 初步学习