Remove Duplicates from Sorted List II Show result

LintCode – Remove Duplicates from Sorted List II Show resultWeb Link

Description

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. Example Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3.

Code – C++/** * Definition of ListNode * class ListNode { * public: *int val; *ListNode *next; *ListNode(int val) { *this->val = val; *this->next = NULL; *} * } */class Solution{public:/*** @param head: The first node of linked list.* @return: head node*/ListNode * deleteDuplicates(ListNode *head) {// write your code hereif (head == NULL || head->next == NULL) {return head;}ListNode* dummy = new ListNode(0);dummy->next = head;head = dummy;while (head->next != NULL && head->next->next != NULL) {if (head->next->val == head->next->next->val) {int val = head->next->val;while (head->next != NULL && head->next->val == val) {head->next = head->next->next;}} else {head = head->next;}}return dummy->next;}};Tips

None

,我喜欢出发。凡是到达了的地方,

Remove Duplicates from Sorted List II Show result

相关文章:

你感兴趣的文章:

标签云: