力扣:234. 回文链表
This commit is contained in:
parent
811bd71ed9
commit
4723f25d7a
@ -0,0 +1,30 @@
|
|||||||
|
package com.code.leet.study.t20210207;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.ListNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 请判断一个链表是否为回文链表。
|
||||||
|
*/
|
||||||
|
public class IsPalindrome {
|
||||||
|
public boolean isPalindrome(ListNode head) {
|
||||||
|
boolean bl = true;
|
||||||
|
List<Integer> list = new ArrayList<>();
|
||||||
|
while (head != null) {
|
||||||
|
list.add(head.val);
|
||||||
|
head = head.next;
|
||||||
|
}
|
||||||
|
int headIndex = 0;
|
||||||
|
int size = list.size();
|
||||||
|
while (headIndex < size - headIndex - 1) {
|
||||||
|
if (!list.get(headIndex).equals(list.get(size - headIndex - 1))) {
|
||||||
|
bl = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
headIndex++;
|
||||||
|
}
|
||||||
|
return bl;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user