leetcode 083 —— Remove Duplicates from Sorted List

来源:互联网 发布:顶易软件怎么样 编辑:程序博客网 时间:2024/06/03 22:53

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.


思路:双指针游走遍历

class Solution {public:ListNode* deleteDuplicates(ListNode* head) {if (!head || !head->next) return head;ListNode *cur = head;ListNode *next = head->next;while (cur&&next){while (next&&cur->val == next->val)  //next往后寻找不得cur相同的元素next = next->next;if (cur->next = next){cur = cur->next;next = next->next;}else{cur->next = next;cur = next;if (next)next = next->next;}}return head;}};


0 0