LeetCode – 82. Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

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

This problem requires using a dummy pointer and then fast and slow pointers.

/** * Definition for singly-linked list. * public class ListNode { * int val; null){ while(fast.next != null && fast.val == fast.next.val){ fast = fast.next; } if(slow.next == fast){ slow = slow.next; fast = fast.next; } else{ slow.next = fast.next; fast = fast.next; } } return dummy.next; } }

This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:

Original author:Jake Tao,source:"LeetCode – 82. Remove Duplicates from Sorted List II"

148
0 0 148

Further Reading

Post a reply

Log inYou can only comment after that.
Share this page
Back to top