Skip to content

Commit fd28d53

Browse files
committed
leetcode
1 parent 04ede68 commit fd28d53

File tree

4 files changed

+377
-0
lines changed

4 files changed

+377
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
3+
4+
5+
-* Palindrome Linked List *-
6+
7+
Given the head of a singly linked list, return true if it is a palindrome or false otherwise.
8+
9+
10+
11+
Example 1:
12+
13+
14+
Input: head = [1,2,2,1]
15+
Output: true
16+
Example 2:
17+
18+
19+
Input: head = [1,2]
20+
Output: false
21+
22+
23+
Constraints:
24+
25+
The number of nodes in the list is in the range [1, 105].
26+
0 <= Node.val <= 9
27+
28+
29+
Follow up: Could you do it in O(n) time and O(1) space?
30+
31+
*/
32+
33+
// Definition for singly-linked list.
34+
class ListNode {
35+
int val;
36+
ListNode? next;
37+
ListNode([this.val = 0, this.next]);
38+
}
39+
40+
class A {
41+
bool isPalindrome(ListNode? head) {
42+
// to store the values
43+
List<int> array = [];
44+
// if there is value we will add into our list
45+
if (head != null) {
46+
array.add(head.val);
47+
// pointing it to the next value
48+
head = head.next;
49+
}
50+
// now all the values is inside the list so let's work on list
51+
// - pointer on the lift value
52+
int left = 0;
53+
// pointer on the right value
54+
int right = array.length - 1;
55+
// assuming that he value on left side is less than the right side
56+
while (left < right) {
57+
// reading forward values from left side via ++
58+
// reading it backward values via --
59+
60+
// from forward to backward are not same than it's not palindrome
61+
if (array[left++] != array[right--]) return false;
62+
}
63+
return true;
64+
}
65+
}
66+
67+
class B {
68+
// Runtime: 628 ms, faster than 51.02% of Dart online submissions for Palindrome Linked List.
69+
// Memory Usage: 194.6 MB, less than 69.39% of Dart online submissions for Palindrome Linked List.
70+
bool isPalindrome(ListNode? head) {
71+
// if everything is null than it's true means no value available
72+
if (head == null || head.next == null) return true;
73+
74+
// first value on the head
75+
int first = head.val;
76+
// second value both needs to compare
77+
int second = head.val;
78+
// multiplier
79+
int currentMultiplier = 10;
80+
// while nothing is null means we have values
81+
while ((head = head?.next) != null) {
82+
// simple multiplication and summation
83+
first = (first * 10) + head!.val;
84+
second = second + (currentMultiplier * head.val);
85+
currentMultiplier *= 10;
86+
}
87+
88+
// if both are same boom Congratulation
89+
return first == second;
90+
}
91+
}
92+
93+
class C {
94+
// Runtime: 595 ms, faster than 63.27% of Dart online submissions for Palindrome Linked List.
95+
// Memory Usage: 194.5 MB, less than 69.39% of Dart online submissions for Palindrome Linked List.
96+
ListNode reverseLL(ListNode head) {
97+
ListNode? prev = null, next = null, cur = head;
98+
while (cur != null) {
99+
// creating duplicate of every node during iteration
100+
ListNode temp = new ListNode(cur.val, cur.next);
101+
102+
// swapping prev & next of 'temp' node
103+
next = temp.next;
104+
temp.next = prev;
105+
prev = temp;
106+
cur = next; // moving ahead in original LL
107+
}
108+
return prev!;
109+
}
110+
111+
bool isPalindrome(ListNode? head) {
112+
ListNode? tail = reverseLL(head!); // got the head of reversed LL
113+
114+
while (head != null) {
115+
if (head.val != tail?.val) return false;
116+
head = head.next;
117+
tail = tail?.next;
118+
}
119+
return true;
120+
}
121+
}
122+
123+
class Solution {
124+
bool isPalindrome(ListNode? head) {
125+
ListNode? temp = head;
126+
127+
ListNode? midNode = getMiddle(temp!);
128+
ListNode? tail = reverseLL(midNode);
129+
130+
while (tail != null) {
131+
if (tail.val != head?.val) return false;
132+
tail = tail.next;
133+
head = head?.next;
134+
}
135+
return true;
136+
}
137+
138+
ListNode getMiddle(ListNode head) {
139+
// using tortoise-hare method to find middle element
140+
ListNode? prevTort = null, tort = head, hare = head;
141+
while (hare != null && hare.next != null) {
142+
prevTort = tort;
143+
tort = tort?.next;
144+
hare = hare.next?.next;
145+
}
146+
if (tort != null) // case when LL is of odd length
147+
return tort;
148+
return prevTort!; // case of even length we'll get 2 mid. So, returning 1st mid node.
149+
}
150+
151+
ListNode reverseLL(ListNode head) {
152+
ListNode? prev = null, next = null, cur = head;
153+
while (cur != null) {
154+
next = cur.next;
155+
cur.next = prev;
156+
prev = cur;
157+
cur = next;
158+
}
159+
return prev!;
160+
}
161+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
// Definition for singly-linked list.
4+
type ListNode struct {
5+
Val int
6+
Next *ListNode
7+
}
8+
9+
// func isPalindrome(head *ListNode) bool {
10+
// return palindrome(head, head)
11+
// }
12+
13+
// func palindrome(head, tail *ListNode) bool {
14+
// if tail.Next != nil {
15+
// if !palindrome(head, tail.Next) { // jump to the last Node
16+
// return false
17+
// }
18+
// }
19+
// if head.Val != tail.Val {
20+
// return false
21+
// }
22+
// if head != tail { // move head to next
23+
// *head = *head.Next
24+
// }
25+
// return true
26+
// }
27+
28+
// func isPalindrome(head *ListNode) bool {
29+
// stack, next := half(head)
30+
// for i := len(stack) - 1; next != nil; i-- {
31+
// if stack[i].Val != next.Val {
32+
// return false
33+
// }
34+
// next = next.Next
35+
// }
36+
// return true
37+
// }
38+
39+
// func half(head *ListNode) ([]*ListNode, *ListNode) {
40+
// stack := make([]*ListNode, 0, 8)
41+
// for fast := head; fast != nil && fast.Next != nil; head = head.Next {
42+
// stack = append(stack, head)
43+
// fast = fast.Next.Next
44+
// if fast == nil {
45+
// break
46+
// }
47+
// }
48+
// return stack, head.Next
49+
// }
50+
51+
func isPalindrome(head *ListNode) bool {
52+
for left, right := partition2(head); left != nil; left, right = left.Next, right.Next {
53+
if left.Val != right.Val {
54+
return false
55+
}
56+
}
57+
return true
58+
}
59+
60+
func partition2(head *ListNode) (left *ListNode, right *ListNode) {
61+
right = head.Next
62+
for fast := head; fast.Next != nil; {
63+
fast = fast.Next.Next
64+
// Critical, reverse operation must happens after fast forwarding.
65+
// Reverse the left half side
66+
left, head.Next = head, left
67+
if fast == nil {
68+
break
69+
}
70+
// Critical, right is faster 1 step than head
71+
// Forward to the front of the right half side
72+
head, right = right, right.Next
73+
}
74+
return
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# 🔥 Palindrome Linked List 🔥 || 4 Solutions || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1 Two Pointers
4+
5+
```dart
6+
class Solution {
7+
bool isPalindrome(ListNode? head) {
8+
// to store the values
9+
List<int> array = [];
10+
// if there is value we will add into our list
11+
if (head != null) {
12+
array.add(head.val);
13+
// pointing it to the next value
14+
head = head.next;
15+
}
16+
// now all the values is inside the list so let's work on list
17+
// - pointer on the lift value
18+
int left = 0;
19+
// pointer on the right value
20+
int right = array.length - 1;
21+
// assuming that he value on left side is less than the right side
22+
while (left < right) {
23+
// reading forward values from left side via ++
24+
// reading it backward values via --
25+
26+
// from forward to backward are not same than it's not palindrome
27+
if (array[left++] != array[right--]) return false;
28+
}
29+
return true;
30+
}
31+
}
32+
```
33+
34+
## Solution - 2 Mathematical
35+
36+
```dart
37+
class Solution {
38+
// Runtime: 628 ms, faster than 51.02% of Dart online submissions for Palindrome Linked List.
39+
// Memory Usage: 194.6 MB, less than 69.39% of Dart online submissions for Palindrome Linked List.
40+
bool isPalindrome(ListNode? head) {
41+
// if everything is null than it's true means no value available
42+
if (head == null || head.next == null) return true;
43+
44+
// first value on the head
45+
int first = head.val;
46+
// second value both needs to compare
47+
int second = head.val;
48+
// multiplier
49+
int currentMultiplier = 10;
50+
// while nothing is null means we have values
51+
while ((head = head?.next) != null) {
52+
// simple multiplication and summation
53+
first = (first * 10) + head!.val;
54+
second = second + (currentMultiplier * head.val);
55+
currentMultiplier *= 10;
56+
}
57+
58+
// if both are same boom Congratulation
59+
return first == second;
60+
}
61+
}
62+
```
63+
64+
## Solution - 3 Recursive
65+
66+
```dart
67+
class Solution {
68+
// Runtime: 595 ms, faster than 63.27% of Dart online submissions for Palindrome Linked List.
69+
// Memory Usage: 194.5 MB, less than 69.39% of Dart online submissions for Palindrome Linked List.
70+
ListNode reverseLL(ListNode head) {
71+
ListNode? prev = null, next = null, cur = head;
72+
while (cur != null) {
73+
// creating duplicate of every node during iteration
74+
ListNode temp = new ListNode(cur.val, cur.next);
75+
76+
// swapping prev & next of 'temp' node
77+
next = temp.next;
78+
temp.next = prev;
79+
prev = temp;
80+
cur = next; // moving ahead in original LL
81+
}
82+
return prev!;
83+
}
84+
85+
bool isPalindrome(ListNode? head) {
86+
ListNode? tail = reverseLL(head!); // got the head of reversed LL
87+
88+
while (head != null) {
89+
if (head.val != tail?.val) return false;
90+
head = head.next;
91+
tail = tail?.next;
92+
}
93+
return true;
94+
}
95+
}
96+
```
97+
98+
## SOlution - 4 Something Complicated
99+
100+
```dart
101+
class Solution {
102+
bool isPalindrome(ListNode? head) {
103+
ListNode? temp = head;
104+
105+
ListNode? midNode = getMiddle(temp!);
106+
ListNode? tail = reverseLL(midNode);
107+
108+
while (tail != null) {
109+
if (tail.val != head?.val) return false;
110+
tail = tail.next;
111+
head = head?.next;
112+
}
113+
return true;
114+
}
115+
116+
ListNode getMiddle(ListNode head) {
117+
// using tortoise-hare method to find middle element
118+
ListNode? prevTort = null, tort = head, hare = head;
119+
while (hare != null && hare.next != null) {
120+
prevTort = tort;
121+
tort = tort?.next;
122+
hare = hare.next?.next;
123+
}
124+
if (tort != null) // case when LL is of odd length
125+
return tort;
126+
return prevTort!; // case of even length we'll get 2 mid. So, returning 1st mid node.
127+
}
128+
129+
ListNode reverseLL(ListNode head) {
130+
ListNode? prev = null, next = null, cur = head;
131+
while (cur != null) {
132+
next = cur.next;
133+
cur.next = prev;
134+
prev = cur;
135+
cur = next;
136+
}
137+
return prev!;
138+
}
139+
}
140+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
8787
- [Two Sum IV - Input is a BST](TwoSumIV-InputIsABST/two_sum_IV_input_is_a_bst.dart)
8888
- [Valid Anagram](ValidAnagram/valid_anagram.dart)
8989
- [Break a Palindrome](BreakAPalindrome/break_a_palindrome.dart)
90+
- [Palindrome Linked List](PalindromeLinkedList/palindrome_linked_list.dart)
9091

9192
## Reach me via
9293

0 commit comments

Comments
 (0)