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搞定的那种。

相关文章:

  • 2021-09-19
  • 2021-06-02
  • 2022-01-17
  • 2021-12-07
  • 2022-12-23
  • 2021-05-22
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
  • 2021-06-13
  • 2021-11-17
相关资源
相似解决方案