Leetcode234_palindromelinkedlist (Judging whether it is a palindrome list) Java puzzle

Source: Internet
Author: User

Topic:

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could do it in O (n) time and O (1) space?

Exercises

To determine whether a linked list is a palindrome, here requires O (n) time complexity and O (1) Space time complexity, a total of three ways, three methods are used two pointers, in line with the title of the last one.


The first approach: use an array to reverse the value of the list of the first half of the table, and then compare the value of the list with the second half. This solution runs for the longest time possible because the inverted insertion of the array is more time consuming.

Code:

Implement O (N/2) space public static Boolean ispalindrome (ListNode head) {//arraylist<integer> nodeval=new ArrayList with an array <> (); Linkedlist<integer> nodeval=new linkedlist<> ();  if (head==null| | Head.next==null) return true; ListNode Slow=head; ListNode Fast=head;  Nodeval.add (0,slow.val); while (fast.next!=null&&fast.next.next!=null) {fast=fast.next.next; slow=slow.next; NodeVal.add (0,slow.val ); }  ListNode Cur=slow; if (fast.next!=null)//list length is even cur=slow.next; int i=0; while (cur!=null) {if (Nodeval.get (i)! = Cur.val) return false; Cur=cur.next; i++; } return true;    
The second solution: on the basis of the first kind of thinking, we want to achieve a reverse order, we do not use the data structure of the existing-stack, so the first half of the chain list of pressure stack, and then out of the stack and the back of the chain list in turn, the shortest running time, but because the stack or not meet the requirements.

Code:

Use the stack to implement public static Boolean isPalindrome2 (ListNode head) {  stack<listnode> stack=new stack<> (); ListNode Slow=head; ListNode Fast=head;  if (fast==null| | Fast.next==null)//0 nodes or 1 nodes return true; Stack.push (slow); while (fast.next!=null&&fast.next.next!=null) {fast=fast.next.next; slow=slow.next; Stack.push (slow);} if ( Fast.next!=null)//linked list length is even slow=slow.next;  ListNode Cur=slow; while (Cur!=null) {if (Cur.val!=stack.pop (). val) return false; Cur=cur.next;} return true;  }

The Third kind: we think so, we can not use external storage to achieve reverse, in fact, it is possible, the list reversal when we do not use external storage. The idea is to reverse the second half of the in-place list and then compare it to the first half (you can also reverse the first half) and run a little slower than the next, but meet the requirements of the question O (1) Space complexity

Code:

The list of links in place to implement O (1) Space complexity public static Boolean IsPalindrome3 (ListNode head) {ListNode slow=head; ListNode Fast=head;  if (fast==null| | Fast.next==null)//0 nodes or 1 nodes return true; while (fast.next!=null&&fast.next.next!=null) {fast=fast.next.next; slow=slow.next;}//Reverses the second half of the list ListNode Midnode=slow; ListNode firnode=slow.next;//The first node of the second half of the list ListNode cur=firnode.next;//the insertion node begins at the beginning of the first node firnode.next=null;// The first node eventually changes to the last node while (cur!=null) {ListNode nextnode=cur.next;//saves the next traversed node cur.next=midnode.next; midnode.next=cur; Cur=nextnode; }  //Inversion after the first and second half of the comparison slow=head; fast=midnode.next; while (fast!=null) {if (fast.val!=slow.val) return false; slow= Slow.next; Fast=fast.next; } return true;  




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode234_palindromelinkedlist (Judging whether it is a palindrome list) Java puzzle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.