Skip to content

Commit 62954de

Browse files
committed
solve problem Palindrome Linked List
1 parent 510847d commit 62954de

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ All solutions will be accepted!
150150
|111|[Minimum Depth Of Binary Tree](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/)|[java/py/js](MinimumDepthOfBinaryTree)|Easy|
151151
|849|[Maximize Distance To Closest Person](https://leetcode-cn.com/problems/maximize-distance-to-closest-person/description/)|[java/py/js](./algorithms/MaximizeDistanceToClosestPerson)|Easy|
152152
|674|[Longest Continuous Increasing Subsequence](https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/description/)|[java/py/js](./algorithms/LongestContinuousIncreasingSubsequence)|Easy|
153+
|234|[Palindrome Linked List](https://leetcode-cn.com/problems/palindrome-linked-list/description/)|[java/py/js](./algorithms/PalindromeLinkedList)|Easy|
153154

154155
# Database
155156
|#|Title|Solution|Difficulty|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Palindrome Linked List
2+
First get the length of linked list, then reverse the half of the linked list and compare two linked list
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode(int x) { val = x; }
7+
* }
8+
*/
9+
class Solution {
10+
public boolean isPalindrome(ListNode head) {
11+
int length = 0,
12+
index = 0;
13+
ListNode p = head;
14+
15+
while (p != null) {
16+
length++;
17+
p = p.next;
18+
}
19+
20+
p = null;
21+
22+
while (index < length / 2) {
23+
index++;
24+
ListNode temp = p;
25+
p = head;
26+
head = head.next;
27+
p.next = temp;
28+
}
29+
30+
if (length % 2 == 1) {
31+
head = head.next;
32+
}
33+
34+
while (p != null && head != null) {
35+
if (p.val != head.val) {
36+
return false;
37+
} else {
38+
p = p.next;
39+
head = head.next;
40+
}
41+
}
42+
return true;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {boolean}
11+
*/
12+
var isPalindrome = function(head) {
13+
let length = 0,
14+
index = 0,
15+
p = head
16+
17+
while (p) {
18+
length++
19+
p = p.next
20+
}
21+
22+
p = null
23+
24+
while (index < parseInt(length / 2)) {
25+
index++
26+
let temp = p
27+
p = head
28+
head = head.next
29+
p.next = temp
30+
}
31+
32+
if (length % 2 === 1) {
33+
head = head.next
34+
}
35+
36+
while (p && head) {
37+
if (p.val !== head.val) {
38+
return false
39+
} else {
40+
p = p.next
41+
head = head.next
42+
}
43+
}
44+
return true
45+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Definition for singly-linked list.
2+
# class ListNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution(object):
8+
def isPalindrome(self, head):
9+
"""
10+
:type head: ListNode
11+
:rtype: bool
12+
"""
13+
length = 0
14+
p = head
15+
# get the length of linked list
16+
while p:
17+
length += 1
18+
p = p.next
19+
20+
# reverse the half of the linked list
21+
index = 0
22+
p = None
23+
while index < length / 2:
24+
index += 1
25+
p, head, p.next = head, head.next, p
26+
27+
if length % 2 == 1:
28+
head = head.next
29+
30+
# compare the two linked list
31+
while p and head:
32+
if p.val != head.val:
33+
return False
34+
else:
35+
p = p.next
36+
head = head.next
37+
38+
return True
39+

0 commit comments

Comments
 (0)