题目
回文链表 给你一个单链表的头节点 head ,请你判断该链表是否为回文链表。如果是,返回 true ;否则,返回 false 。
示例 1:
输入:head = [1,2,2,1] 输出:true 示例 2:
输入:head = [1,2] 输出:false
作者:力扣 (LeetCode) 链接:https://leetcode.cn/leetbook/read/top-interview-questions-easy/xnv1oc/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题解
回文链表
三种方法:转为数组 、递归、快慢指针
方法一:转为数组
java解
在java中 列表分为 数组列表 和 链表
// 数组列表底层使用数组存储 可以通过下标访问 复杂度是O(1)(注:数组列表的长度是 数组列表名.size())
// 链表底层存储是一个节点存储下一个节点的位置,访问一个节点的复杂度是O(n)
数组列表的长度数组列表名.size() 数组列表 获取其中一个元素数组列表名.get(i)
class Solution {
public boolean isPalindrome(ListNode head) {
List<Integer> vals = new ArrayList<Integer>();
ListNode currentNode = head;
while(currentNode!=null){
vals.add(currentNode.val);
currentNode = currentNode.next;
}
int front = 0;
int back = vals.size()-1;
while(front<back){
if(!vals.get(front).equals(vals.get(back))){
return false;
}
front++;
back--;
}
return true;
}
}
python解
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
temp = []
while head:
temp.append(head.val)
head = head.next
return temp == temp[::-1]
方法二:递归
由下面的代码 可以知道 递归是从后往前的
function print_values_in_reverse(ListNode head)
if head is NOT null
print_values_in_reverse(head.next)
print head.val
利用这个由后往前的特性,我们可以与一个从前往后的指针比较。·
java解
class Solution {
private ListNode frontPointer;
private boolean recursivelyCheck(ListNode currentNode){
if(currentNode!=null){
if(!recursivelyCheck(currentNode.next)){
return false;
}
if(currentNode.val != frontPointer.val){
return false;
}
frontPointer = frontPointer.next;
}
return true;
}
public boolean isPalindrome(ListNode head) {
frontPointer = head;
return recursivelyCheck(head);
}
}
python解
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
self.front_pointer = head
def recursively_check(current_node=head):
if current_node is not None:
if not recursively_check(current_node.next):
return False
if self.front_pointer.val!=current_node.val:
return False
self.front_pointer = self.front_pointer.next
return True
return recursively_check()
方法三:快慢指针
java解
class Solution {
public boolean isPalindrome(ListNode head) {
if(head == null){
return true;
}
ListNode firstHalfEnd = endOfFirstHalf(head);
ListNode secondHalfStart = reverseList(firstHalfEnd.next);
ListNode p1 = head;
ListNode p2 = secondHalfStart;
boolean result = true;
while (result && p2!=null){
if(p1.val != p2.val){
result = false;
}
p1 = p1.next;
p2 = p2.next;
}
firstHalfEnd.next = reverseList(secondHalfStart);
return result;
}
private ListNode reverseList(ListNode head){
ListNode prev = null;
ListNode curr = head;
while(curr != null){
ListNode nextTemp = curr.next;
curr.next = pre;
pev = curr;
curr = nextTemp;
}
return prev;
}
private ListNode endOfFirstHalf(ListNode head){
ListNode fast = head;
ListNode slow = head;
while(fast.next !=null && slow.next!=null){
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
}
python解
这个python代码好像不太对 后面再改改
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
if head is None:
return True
first_half_end = self.end_of_first_half(head)
second_half_start = self.reverse_list(first_half_end.next)
result = True
first_position = head
second_position = second_half_start
while result and second_position is not None:
if first_position.val != second_position.val:
result = False
first_position = first_position.next
second_position = second_position.next
first_half_end.next = self.reverse_list(second_half_start)
return result
def end_of_first_half(self,head):
fast = head
slow = head
while fast.next is not None and fast.next.next is not None:
fast = fast.next.next
slow = slow.next
return slow
def reverse_list(self,head):
previous = None
current = head
while current.next is not None :
next_node = current.next
current.next = previous
previous = current
current = next_node
return previous
|