82. Remove Duplicates from Sorted List II [medium]
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
/**
* 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* ret, *fast = head, *dummy = new ListNode(-1), *slow = dummy;
int same;
while(fast != NULL and fast->next != NULL) {
if (fast->val == fast->next->val) {
same = fast->val;
while(fast && fast->val == same) {
fast = fast->next;
}
} else {
slow->next = new ListNode(fast->val);
slow = slow->next;
slow->next = NULL;
fast = fast->next;
}
}
if (fast) {
slow->next = new ListNode(fast->val);
slow = slow->next;
slow->next = NULL;
fast = fast->next;
}
return dummy->next;
}
};
/**
* 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;
if(head->val == head->next->val) {
while(head->next && head->val == head->next->val) {
head = head->next;
}
return deleteDuplicates(head->next);
} else {
head->next = deleteDuplicates(head->next);
}
return head;
}
};
83. Remove Duplicates from Sorted List [easy]
Given a sorted linked list, delete all duplicates such that each element appear only once.
Example 1:
Input: 1->1->2
Output: 1->2
Example 2:
Input: 1->1->2->3->3
Output: 1->2->3
/**
* 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) {
ListNode *ptr = head, *next = NULL;
while(ptr != NULL && ptr->next != NULL) {
next = ptr->next;
if (ptr->val == next->val) {
// skip duplicat node
ptr->next = next->next;
delete(next);
} else {
ptr = ptr->next;
}
}
return head;
}
};
19. Remove Nth Node From End of List [medium]
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
Given linked list: 1->2->3->4->5, and n = 2.
After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* ptr1 = head, *ptr2 = head;
for(int i = 0; i < n; i++) {
ptr1 = ptr1->next;
}
if(ptr1 == NULL) {
// ptr1 at the end of the array, so return [1:]of list
return head->next;
}
while(ptr1->next != NULL && ptr2->next != NULL) {
ptr1 = ptr1->next;
ptr2 = ptr2->next;
}
ListNode* next = ptr2->next;
ptr2->next = next->next;
delete(next);
return head;
}
};