Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Given the head
of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
Example 1:
Input: head = [1,1,2]
Output: [1,2]
Example 2:
Input: head = [1,1,2,3,3]
Output: [1,2,3]
Constraints:
[0, 300]
.-100 <= Node.val <= 100
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null)return head;
head.next = deleteDuplicates(head.next);
return head.val == head.next.val ? head.next : head;
}
def deleteDuplicates(self, head):
cur = head
while cur:
while cur.next and cur.next.val == cur.val:
cur.next = cur.next.next # skip duplicated node
cur = cur.next # not duplicate of current node, move to next node
return head
ListNode* deleteDuplicates(ListNode* head) {
ListNode* cur = head;
while(cur) {
while(cur->next && cur->val == cur->next->val) {
cur->next = cur->next->next;
}
cur = cur->next;
}
return head;
}
In our experience, we suggest you solve this Remove Duplicates from Sorted List LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the Remove Duplicates from Sorted List LeetCode Solution
I hope this Remove Duplicates from Sorted List LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>