-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2095-Delete-the-middle-node-of-a-linked-list.cs
More file actions
63 lines (50 loc) · 1.5 KB
/
2095-Delete-the-middle-node-of-a-linked-list.cs
File metadata and controls
63 lines (50 loc) · 1.5 KB
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
using Common;
using System;
using System.Collections.Generic;
using System.Text;
namespace Solution._2095.Delete_the_middle_node_of_a_linked_list
{
public class _2095_Delete_the_middle_node_of_a_linked_list
{
public ListNode DeleteMiddle(ListNode head)
{
// two pointers
if (head == null || head.next == null) return null;
var fast = head.next.next;
var slow = head;
while (fast != null && fast.next != null)
{
slow = slow.next;
fast = fast.next.next;
}
slow.next = slow.next.next;
return head;
// counting
//if (head == null) return null;
//var curr = head;
//var res = head;
//int index = -1;
//while (curr != null)
//{
// curr = curr.next;
// index++;
//}
//if (index == 0) return new ListNode().AddNode(new int[] { });
//int mid = index % 2 == 0 ? index / 2 : index / 2 + 1;
//int count = 0;
//while (res != null)
//{
// // 2==2
// if (count == mid - 1)
// {
// var temp = res.next.next;
// res.next = temp;
// break;
// }
// count++;
// res = res.next;
//}
//return head;
}
}
}