LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List

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.

 1         public static LinkedListNode RemoveDuplicatesfromSortedList(LinkedListNode head)
 2         {
 3             if (head == null || head.Next == null)
 4                 return head;
 5 
 6             LinkedListNode prev = head;
 7             LinkedListNode curr = head.Next;
 8 
 9             while(curr != null)
10             {
11                 if ((int)curr.Value == (int)prev.Value)
12                 {
13                     prev.Next = curr.Next;
14                     curr = curr.Next;
15                 }
16                 else
17                 {
18                     prev = curr;
19                     curr = curr.Next;
20                 }
21             }
22             return head;
23         }

代码分析:

  不难,一个prev,一个curr搞定的那种。