-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path83.DeleteDuplicates.cs
46 lines (45 loc) · 1.16 KB
/
83.DeleteDuplicates.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//83. Remove Duplicates from Sorted List
//Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
public class listnode
{
public int val;
public listnode next;
public listnode(int val = 0, listnode next = null)
{
this.val = val;
this.next = next;
}
}
public class Solution
{
public listnode DeleteDuplicates(listnode head)
{
listnode pivot = head;
while (pivot != null)
{
while (pivot.next != null && pivot.val == pivot.next.val)
{
pivot.next = pivot.next.next;
}
pivot = pivot.next;
}
return head;
}
}
public class Solution {
public ListNode DeleteDuplicates(ListNode head) {
ListNode cur = head;
while (cur != null && cur.next !=null)
{
if(cur.val == cur.next.val)
{
cur.next = cur.next.next;
}
else
{
cur = cur.next;
}
}
return head;
}
}