赞
踩
示例 1:
输入:head = [1,2,2,1]
输出:true
示例 2:
输入:head = [1,2]
输出:false
package LinkedList; public class p234_PalindromicLinkedList { int val; p234_PalindromicLinkedList next; public p234_PalindromicLinkedList() { } public p234_PalindromicLinkedList(int val) { this.val = val; } public p234_PalindromicLinkedList(int val, p234_PalindromicLinkedList next) { this.val = val; this.next = next; } public static void main(String[] args) { p234_PalindromicLinkedList node1 = new p234_PalindromicLinkedList(1); p234_PalindromicLinkedList node2 = new p234_PalindromicLinkedList(2); p234_PalindromicLinkedList node3 = new p234_PalindromicLinkedList(2); p234_PalindromicLinkedList node4 = new p234_PalindromicLinkedList(1); node1.next = node2; node2.next = node3; node3.next = node4; boolean res = isPalindrome(node1); System.out.println(res); } public static boolean isPalindrome(p234_PalindromicLinkedList head) { p234_PalindromicLinkedList slow = head, fast = head, pre = null, temp = null; while (fast != null) { slow = slow.next; fast = fast.next != null ? fast.next.next : fast.next; } while (slow != null) { temp = slow.next; slow.next = pre; pre = slow; slow = temp; } while (head != null && pre != null) { if (head.val != pre.val) { return false; } head = head.next; pre = pre.next; } return true; } }
#include<stdio.h> #include<stdbool.h> struct ListNode { int val; struct ListNode *next; }; bool p234_PalindromicLinkedList_isPalindrome(struct ListNode* head) { struct ListNode *slow = head, *fast = head, *pre = NULL, *temp = NULL; while (fast != NULL) { slow = slow->next; fast = fast->next ? fast->next->next : fast->next; } while (slow != NULL) { temp = slow->next; slow->next = pre; pre = slow; slow = temp; } while (head != NULL && pre != NULL) { if (head->val != pre->val) { return false; } head = head->next; pre = pre->next; } return true; } /*主函数省略*/
Java语言版
C语言版
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。