-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleetcode_234_Palindrome_Linked_ListCS.cs
151 lines (122 loc) · 4.49 KB
/
leetcode_234_Palindrome_Linked_ListCS.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
https://leetcode.com/problems/palindrome-linked-list
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2
Output: false
Example 2:
Input: 1->2->2->1
Output: true
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace leetcode_234_Palindrome_Linked_ListCS
{
public class ListNode
{
public int val;
public ListNode next;
public ListNode(int x) { val = x; }
}
class Program
{
public static ListNode build_singlylinkedlist_allocatememory(int[] build_singlylinkedlist, int first_element, int last_element)
{
// helper function to allocate memory to the node
ListNode head = null;
if ((build_singlylinkedlist.Count() != 0) && (first_element <= last_element))
{
// create a new node to store value
head = new ListNode(build_singlylinkedlist[first_element]);
head.next = build_singlylinkedlist_allocatememory(build_singlylinkedlist, first_element + 1, last_element);
return head;
}
return head;
}
public static ListNode build_singlylinkedlist(int[] build_singlylinkedlist)
{
// build the linked-list recursively
ListNode head = null;
// declare a ListNode referance to store the start to the node
int first_element = build_singlylinkedlist.GetLowerBound(0);
int last_element = build_singlylinkedlist.GetUpperBound(0);
// calculate the start and end index of the array
head = build_singlylinkedlist_allocatememory(build_singlylinkedlist, first_element, last_element);
// call the helper function to allocate memory to the linked node .
return head;
}
public static void traverse_linkedlist(ListNode head)
{
ListNode temp_head = head;
// declare a referance to the head node for manipulation
while (temp_head != null)
{
// print the data if the node is not null
Console.Write(temp_head.val + " ");
temp_head = temp_head.next;
}
}
/*
* algorithm :
base case:
1. null node or single node -----> true [palindrome]
recursive case
visit the list recursively till the very end
compare the last node and firt node and return the response to the calling statement.
*/
public static ListNode root;
// referance to the head to make the comparisions
public static bool IsPalindrome(ListNode head)
{
root = head;
if (head == null)
{
return true;
}
else
{
bool flag = _isPalindrome(head);
return flag;
}
}
public static Boolean _isPalindrome(ListNode head)
{
Boolean flag = true;
if (head.next != null)
{
flag = _isPalindrome(head.next);
}
if (flag && root.val == head.val)
{
root = root.next;
return true;
}
return false;
}
static void Main(string[] args)
{
int[] linked_arr_collection = new int[] { 1, 2, 3, 2, 1 };
// given array containing the data for the linked list
ListNode head = build_singlylinkedlist(linked_arr_collection);
// call to the function to build the singly linked list in memory
Console.WriteLine("the nodes in the linked list : ");
traverse_linkedlist(head);
// traverse the list
Console.WriteLine();
bool flag = IsPalindrome(head);
// call the function to check weather the given linked linked is a valid pallindrome or not a pallindrome
if (flag == true)
{
Console.WriteLine("linked list a valid pallindrome");
}
else
{
Console.WriteLine("linked list not a valid pallindrome");
}
Console.ReadLine();
}
}
}