# C#LeetCode刷题之#83-删除排序链表中的重复元素（Remove Duplicates from Sorted List）

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

Input: 1->1->2

Output: 1->2

Input: 1->1->2->3->3

Output: 1->2->3

```public class Program {

public static void Main(string[] args) {
var head = new ListNode(1) {
next = new ListNode(1) {
next = new ListNode(2) {
next = new ListNode(3) {
next = new ListNode(3)
}
}
}
};

var res = DeleteDuplicates(head);
ShowArray(res);

Console.ReadKey();
}

private static void ShowArray(ListNode list) {
var node = list;
while(node != null) {
Console.Write(\$"{node.val} ");
node = node.next;
}
Console.WriteLine();
}

private static ListNode DeleteDuplicates(ListNode head) {
if(head == null) return null;
var cur = head;
var next = cur.next;
while(cur != null && next != null) {
while(next != null && cur.val == next.val) {
next = next.next;
}
cur.next = next;
cur = next;
next = cur?.next;
}
return head;
}

public class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}

}```

`1 2 3`

(2)