-
Notifications
You must be signed in to change notification settings - Fork 0
/
DeleteDataFromLinkedList.cs
76 lines (66 loc) · 1.55 KB
/
DeleteDataFromLinkedList.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
72
73
74
75
76
using System;
using System.Text;
namespace ConsoleApp19
{
class Program
{
class Node
{
public int data;
public Node next;
}
static Node Delete(Node head)
{
if (head == null)
return null;
if (head.next == null)
return null;
Node slow_ptr = head;
Node fast_ptr = head;
Node prev = null;
while (fast_ptr != null && fast_ptr.next != null)
{
fast_ptr = fast_ptr.next.next;
prev = slow_ptr;
slow_ptr = slow_ptr.next;
}
prev.next = slow_ptr.next;
return head;
}
static void PrintList(Node ptr)
{
while(ptr != null)
{
Console.Write(ptr.data + "->");
ptr = ptr.next;
}
Console.WriteLine("NULL");
}
static Node NewNode(int data)
{
Node temp = new Node
{
data = data,
next = null
};
return temp;
}
public static void Main()
{
Node head = NewNode(1);
head.next = NewNode(2);
head.next.next = NewNode(3);
head.next.next.next = NewNode(4);
head.next.next.next.next = NewNode(5);
head.next.next.next.next.next = NewNode(6);
head.next.next.next.next.next.next = NewNode(7);
head.next.next.next.next.next.next.next = NewNode(8);
head.next.next.next.next.next.next.next.next = NewNode(9);
Console.WriteLine("Given linked List");
PrintList(head);
head = Delete(head);
Console.WriteLine("Given linked List - after deletion of middle");
PrintList(head);
}
}
}