Monday, August 4, 2014

Remove Duplicates from Sorted List

Prbolem

Given a sorted linked list, delete all duplicates such that each element appear only once.

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

Ideas

Use nested loops to solve the problem. One loop is used to find the different node.

Note

Data Structure Definition
  • Don't use delete to remove useless node.
  • Don't define two pointer for current node and next node because it's difficult to handle the last node.

Solution


Special Case

  • {1,1}
  • {1,1,1}
  • {1}

No comments:

Post a Comment