码迷,mamicode.com
首页 > 其他好文 > 详细

*Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)

时间:2018-08-05 11:51:59      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:atl   int   a star   between   follow   while   amazon   div   term   

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

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

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

Sloving with O(3n) -> O(n)

Idea: reversing the list and match

probelm 1: how to find the middle node of list:

         //1: find the middle node : slow and fast
        ListNode slow = head;
        ListNode fast = head;
        while(fast !=null && fast.next!= null){// fast could be null(odd list) or last node (even list)
            fast = fast.next.next;
            slow = slow.next;
        }
        if(fast != null){ // odd node
            slow = slow.next;
        }

problem 2: how to reverse the list

        //output slow as head node of second half list
        //reverse the second half list
        ListNode end = null, temp; //end: end of second hald list, temp 
        while(slow != null){
            temp = slow.next;
            slow.next = end;
            //temp.next = slow;
            end = slow;
            slow = temp;
        }
        slow = end;    //take end as a start of 2nd list

last code snippest: compare the two part until the 2nd is null

        while(slow!=null){
            if(slow.val == head.val){
                slow = slow.next;
                head = head.next;
                //System.out.println("iside loop:"+slow.val);
            }
            else return false;
        }
        
        return true;    

 

Toatlly:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {//head means the first node
        //boundary case:
        if(head == null || head.next==null) return true;
        if(head.next.next==null) return head.val==head.next.val;
        //reverse the half list and compare
        
        //1: find the middle node : slow and fast
        ListNode slow = head;
        ListNode fast = head;
        while(fast !=null && fast.next!= null){// fast could be null or last node
            fast = fast.next.next;
            slow = slow.next;
        }
        if(fast != null){ // odd node 
            slow = slow.next;
        }
        System.out.println(slow.val);
        //output slow as head node of second half list
        //reverse the second half list
        ListNode end = null, temp; //end: end of second hald list, temp 
        while(slow != null){
            temp = slow.next;
            slow.next = end;
            //temp.next = slow;
            end = slow;
            slow = temp;
        }
        slow = end;
        System.out.println(slow.val);
        //slow id the start node of the 2nd list
        while(slow!=null){
            if(slow.val == head.val){
                slow = slow.next;
                head = head.next;
                //System.out.println("iside loop:"+slow.val);
            }
            else return false;
        }
        
        return true;
        //compare,input:  head and tail
    }
}

 In the linkedlist, know the diff between temp.next = slow |  temp = slow(temp(as a pointer) point to slow)

 Also reference is https://blog.csdn.net/liuchonge/article/details/73658088

 

If you could slove this problem: you cna simply slove 206. Reverse Linked List iteratively

 

*Amazon problem: 234. Palindrome Linked List (reverse the linked list with n time)

标签:atl   int   a star   between   follow   while   amazon   div   term   

原文地址:https://www.cnblogs.com/stiles/p/leetcode234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!