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
- 思路1:相同的数都是相邻的。所以一个fast指针来寻找。如果当前和下一个节点数值相同,就记录这个值,让fast指针走到下一个不为这个值的地方。否则就可以把当前节点加入返回链表之中。最后fast如果不为NULL,说明走到最后一个节点,而且最后一个节点数值不重复,把最后一个节点也要加入返回链表中。
- 注意:
- 可以添加一个头节点,这样会方便一点。
- 新创建节点会简单一点。
/**
* 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;
}
};
- 思路2:看到别人还有一种递归的方法。如果当前和下一个相同,就一直往下找到不同的。否则把这个节点加到返回链表之中。然后再递归处理下一个节点。
- 注意:比较tricky。
/**
* 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?
- 思路:去除倒数第二个数字,而且要一遍。不过保证n是有效的。所以考虑就两个指针。第一个指针走了n步之后第二个开始走。第一个指针走到底的时候,第二个指针也就走到倒数第二个位置了。这个时候删除就可以。
- 注意:
- 链表是没有头节点的。
- 如果只有一个元素,或者说要删除第一个元素。这样第一个节点走n步之后就到头了。这个时候直接返回head->next。
/**
* 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;
}
};