给定一个链表,请判断该链表是否为回文结构。
回文是指该字符串正序逆序完全一致。
数据范围: 链表节点数 ,链表中每个节点的值满足
class Solution { public: bool isPail(ListNode* head) { int a[1000005]; ListNode* now = head; int len = 0; while(now != nullptr) { a[len++] = now->val; now = now->next; } for (int i=0, j=len-1; i<=j; i++, j--) { if (a[i] != a[j]) return false; } return true; } };
import java.util.*; public class Solution { public boolean isPail (ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } ListNode reverseList = reverse(slow); ListNode cur1 = reverseList, cur2 = head; while (cur1 != null) { if (cur1.val != cur2.val) { return false; } cur1 = cur1.next; cur2 = cur2.next; } return true; } public ListNode reverse(ListNode head) { ListNode cur = head, pre = null; while (cur != null) { ListNode tmp = cur.next; cur.next = pre; pre = cur; cur = tmp; } return pre; } }
class Solution: def isPail(self , head: ListNode) -> bool: if not head&nbs***bsp;not head.next: return True # write code here # 1.找到中点 fast, slow = head, head slow_pre = head while fast and fast.next: fast = fast.next.next slow_pre = slow slow = slow.next # 2.slow 会在中间靠右的地方 # [1, 2, 2, 1] => slow 右边的 2 # [1, 2, 3, 2, 1] => slow 在正中间 # 断开链表 slow_pre.next = None # 2.翻转链表 def reverse(node): prev = None while node: tmp = node.next node.next = prev prev = node node = tmp return prev slow = reverse(slow) # 因为 head 会少一些 while head: if slow.val != head.val: return False slow = slow.next head = head.next return True
import java.util.*; public class Solution { /** * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { ListNode slow = head; ListNode fast = head; while(fast != null && fast.next != null){ fast = fast.next.next; slow = slow.next; } Stack<Integer> stack = new Stack<>(); while(slow != null){ stack.add(slow.val); slow = slow.next; } while(!stack.isEmpty()){ if(stack.pop() != head.val){ return false; } head = head.next; } return true; } }
# class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param head ListNode类 the head # @return bool布尔型 # class Solution: def isPail(self , head: ListNode) -> bool: # write code here if not head: return head pre = head nums = [] while pre is not None: val = pre.val pre = pre.next nums.append(val) if nums == nums[::-1]: return True return False
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { //取得链表的长度,再从中间断开,判断是不是一样的 // write code here ArrayList<Integer> list=new ArrayList<Integer>(); ListNode cur=head; while(cur!=null){//遍历整个链表到list,就是为了获得整个链表的长度 list.add(cur.val); cur=cur.next; } boolean ans=true; for(int i=0;i<(list.size()-1)/2;i++){ if(list.get(i)!=(int)list.get(list.size()-1-i)){//注意这里强转为int ans=false; break; } } return ans; } }
bool isPail(struct ListNode* head ) { // write code here int size=0; struct ListNode* p=head; while(p){ size++; p=p->next; } int *arr=malloc(sizeof(int)*size); p=head; int i=0,j=size-1; while(p){ arr[i]=p->val; p=p->next; i++; } i=0; while(i<j){ if(arr[i]==arr[j]){ i++; j--; } else{ return false; } } return true; }
public class Solution { /** * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { // write code here if(head == null){ return false; } // 1: 利用回文链表正反遍历相同的特性将链表节点放入栈中 Stack<ListNode> stack = new Stack<>(); ListNode cur = head; while(cur != null){ stack.add(cur); cur = cur.next; } // 2: 从链表的头节点和栈的顶开始遍历,若每个值都相等说明是回文 cur = head; while(cur != null){ if(cur.val != stack.pop().val){ return false; } cur = cur.next; } return true; } }
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { // write code here ArrayList<ListNode> a = new ArrayList<>(); while (head != null) { a.add(head); head = head.next; } for (int i = 0, j = a.size() - 1; i < a.size() / 2; i++, j--) if (a.get(i).val != a.get(j).val) return false; return true; } }
//快慢指针找中间节点加上慢指针头插法 public boolean isPail (ListNode head) { if (head == null || head.next == null) return true; ListNode fast = head; ListNode slow = head; //慢指针找中间节点 ListNode pre = null; //头插法指针 while(fast != null && fast.next != null){ //不能用|| 当fast=null fast.next会越界 ListNode temp = slow.next; fast = fast.next.next; slow.next = pre; pre = slow; slow = temp; } if (fast != null) slow = slow.next; //奇数个节点,就跳到下一个 while(slow != null && pre != null){ if (pre.val != slow.val) return false; slow = slow.next; pre = pre.next; } return true; }
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param head ListNode类 the head * @return bool布尔型 */ #include <stdbool.h> typedef struct ListNode Node; bool isPail(struct ListNode* head ) { // write code here if(head == NULL || head ->next == NULL){ return true; } Node *p1 = head; Node *p2 = head; //首先找到中点,当p2->next->next为 NULL,那么此时p1刚好在中点 while(p2 != NULL && p2->next != NULL){ p2 = p2->next->next; p1 = p1->next; } //快指针到尾判断,如果p2不为空,说明链表的长度是奇数个 if(p2 != NULL) { p1 = p1->next; } Node *p = p1; Node *pre = NULL; while(p != NULL){ Node *q = p->next; p->next = pre; pre = p; p = q; } while(pre != NULL){ if(pre->val != head->val){ return false; } pre = pre->next; head = head->next; } return true; }
import java.util.*; public class Solution { public boolean isPail (ListNode head) { // 空链表或者链表只有一个节点,是回文链表 if(head == null || head.next == null) return true; // 使用快慢指针,找到中间的节点 ListNode slow = head, fast = head.next; while(fast.next != null && fast.next.next != null){ fast = fast.next.next; slow = slow.next; } // 需要注意的是,如果是奇数个节点的情况,慢指针slow的next是中间节点 // 这个中间节点无论是多少,不影响回文判断,因此需要再次向后移动一个位置 ListNode secondSegHead = slow.next; if(fast.next != null) secondSegHead = secondSegHead.next; // 反转链表的后半段,然后比较前半段和后半段的节点数值 // 如果出现不一致的情况,说明不是回文链表;反之则是回文链表 secondSegHead = reverse(secondSegHead); while(secondSegHead != null){ if(head.val != secondSegHead.val) return false; head = head.next; secondSegHead = secondSegHead.next; } return true; } // 反转链表 private ListNode reverse(ListNode secondSegHead){ ListNode pre = null, cur = secondSegHead, next = null; while(cur != null){ next = cur.next; cur.next = pre; pre = cur; cur = next; } return pre; } }方法二:复制原链表,然后直接反转比较:
import java.util.*; public class Solution { public boolean isPail (ListNode head) { // 如果为空链表,或者只有一个节点,是回文链表 if(head == null || head.next == null) return true; // 如果链表多于两个结点,那么复制这张链表 ListNode tmp_head = head; ListNode dummy_node = new ListNode(0); ListNode cur = dummy_node; while(tmp_head != null){ ListNode node = new ListNode(tmp_head.val); cur.next = node; cur = node; tmp_head = tmp_head.next; } // 反转原表 cur = head; ListNode pre = null, next = cur.next; while(cur != null){ next = cur.next; cur.next = pre; pre = cur; cur = next; } // 将反转的链表和复制的链表进行逐节点对比,如果完全相同就是回文链表 // 否则,不是回文链表 cur = dummy_node.next; while(cur != null){ if(cur.val != pre.val) return false; cur = cur.next; pre = pre.next; } return true; } }
bool isPail(ListNode* head) { if(!head||!head->next) return true; ListNode* fast=head; ListNode* slow=head->next; while(fast->next&&fast->next->next) { fast=fast->next->next; slow=slow->next; } //此时p在右半段链表的第一个位置 ListNode* pre=nullptr; ListNode* next=nullptr; while(slow) { next=slow->next; slow->next=pre; pre=slow; slow=next; } while(head&&pre) { if(head->val!=pre->val) return false; head=head->next; pre=pre->next; } return true; }
public boolean isPail(ListNode head) { if (head == null) { return false; } ListNode fast = head; ListNode slow = head; // 双指针获取后半段链表 while (fast.next != null && fast.next.next != null) { fast = fast.next.next; slow = slow.next; } // 获取后半段链表的翻转链表 ListNode lastHalfList = reverseList(slow.next); // 遍历后半段链表,与前半段相比,每个结点都相等,则为回文结构 while (lastHalfList != null) { if (lastHalfList.val != head.val) { return false; } lastHalfList = lastHalfList.next; head = head.next; } return true; } // 翻转链表 public ListNode reverseList(ListNode head) { ListNode temp = null; ListNode reversedList = null; while (head != null) { temp = head.next; head.next = reversedList; reversedList = head; head = temp; } return reversedList; }
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 * 使用字符串辅助,快速判断链表是否是回文结构 */ bool isPail(ListNode* head) { if (head == NULL) { return true; } string s = ""; while(head) { s += (head->val + '0'); head = head->next; } int i = 0; int j = s.size() - 1; while(i < j) { if (s[i++] != s[j--]) { return false; } } return true; } };
public class Solution { /** * 直接用定义:先逆序创建个链表,再依次对比。 * * * @param head ListNode类 the head * @return bool布尔型 */ public boolean isPail (ListNode head) { // write code here if(head==null)return true; ListNode revser = new ListNode(head.val); ListNode cur = head; while(cur.next!=null){ cur = cur.next; ListNode temp = new ListNode(cur.val); temp.next = revser; revser = temp; } cur=head; while(cur!=null){ if(revser.val!=cur.val)return false; cur = cur.next; revser = revser.next; } return true; } }