The solution seems wrong by debugging into the code


  • 0
    L

    Re: 1ms JAVA beats all If you debug into the code, you will find that "temp.next=rev;" massed up the whole list of nodes into the form of x,y,x,y,...x,y. Therefore, the result might incorrect.

    See detail below:

    1. Redefine the ListNode class:
    1. add index property to indicate the position of current node.
    2. add toString() method to show the node position and value in debug view.
    public class ListNode {
        int index;
        int val;
        ListNode next;
    
        ListNode(int x) {
            val = x;
        }
    
        ListNode(int index, int x) {
            val = x;
            this.index = index;
        }
    
        public String toString(){
            return index+":"+val;
        }
    }
    
    1. Write a simple test case with the input of [0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0].
    public class Solution {
        public boolean isPalindrome(ListNode head) {
            if (head ==null || head.next==null) return true;
    
            ListNode rev=null, slow=head, fast=head;
    
            while(fast!=null && fast.next!=null) {
                fast= fast.next.next;
                if (rev==null) {
                    rev = slow;
                    slow = slow.next;
                } else {
                    ListNode temp = slow;
                    slow = slow.next;
                    temp.next=rev;
                    rev= temp;
                }
            }
    
            if (fast!=null) slow=slow.next;
    
            while(slow!=null) {
                if (slow.val!=rev.val) return false;
                slow=slow.next;
                rev=rev.next;
            }
    
            return true;
        }
    
        public static void main(String[] args) {
    
            Solution pll = new Solution();
            ListNode root = new ListNode(0,0);
            root.next = new ListNode(1,1);
            root.next.next = new ListNode(2,2);
            root.next.next.next = new ListNode(3,3);
            root.next.next.next.next = new ListNode(4,4);
            root.next.next.next.next.next = new ListNode(5,5);
            root.next.next.next.next.next.next = new ListNode(6,4);
            root.next.next.next.next.next.next.next = new ListNode(7,3);
            root.next.next.next.next.next.next.next.next = new ListNode(8,2);
            root.next.next.next.next.next.next.next.next.next = new ListNode(9,1);
            root.next.next.next.next.next.next.next.next.next.next = new ListNode(10,0);
    
            assertTrue ("[0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]", pll.isPalindrome(root), true);
        }
    
        private static void assertTrue(String input, boolean output, boolean expected) {
            String msg = "Input:%s%n Output: %b%n Expected: %b%n%n";
            System.out.printf(msg, input, output, expected);
        }
    }
    
    1. Debug the code, before the execution of line "temp.next=rev;" the list nodes looks fine.
      0_1476041088923_02.BeforeExecuteLine20.png

    2. After execution of the line "temp.next=rev;", the values in list were messed up. See screenshot below:
      0_1476041143969_03.AfterExectueLine20.png


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.