-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
71 lines (70 loc) · 2.37 KB
/
Program.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//设置三个指针:dummy,pre和point。遍历链表,只有到point指针的值和其前后指针都不相等时,连接dummy指针和point指针。
//当point.next为空时停止遍历。人为检查是否连接最后一个节点。
using System;
using System.Collections.Generic;
namespace RemoveDuplicatesFromSortedListII
{
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
static ListNode DeleteDuplicates(ListNode head)
{
if (head != null)
{
ListNode res = new ListNode(int.MaxValue);
ListNode pre = res;
pre.next = head;
ListNode point = head;
ListNode dummy = res;
while (point.next != null)
{
if (point.val != pre.val && point.val != point.next.val)
{
dummy.next = point;
dummy = dummy.next;
}
point = point.next;
pre = pre.next;
}
if (point.val != pre.val)
dummy.next = point;
else
dummy.next = null;
return res.next;
}
else
return null;
}
static ListNode DeleteDuplicates_List(ListNode head)
{
if (head == null) return null;
var record = new List<ListNode>();
var remove = false;
while (head != null)
{
if (record.Count == 0 || head.val != record[record.Count - 1].val)
{
if (remove) record.RemoveAt(record.Count - 1);
if (record.Count != 0) record[record.Count - 1].next = head;
record.Add(head);
remove = false;
}
else
remove = true;
head = head.next;
}
if (remove) record.RemoveAt(record.Count - 1);
if (record.Count != 0) record[record.Count - 1].next = head;
return record.Count == 0 ? null : record[0];
}
}
}