Friday, February 26, 2016

LeetCode Q82: 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->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

Solution 1 (Non-recursive)
Compare value of current node and next node. Be careful to check if next node is NULL ahead. Also, be careful of some corner case such as:
[1, 1, 2]: Need to create a new head  node in front of the first node.
[1, 1]: Need to create a new head  node in front of the first node.
[1]: Need to create a new head  node in front of the first node.
[1, 2, 2, 3, 3]: when duplicate numbers are one after another, need to double check whether the first non identical number is a new start of next group of duplicate numbers.

Round 2 solution:

No comments:

Post a Comment