What is wrong with my code? I test it in the java project , and it worked.But it cannot work here.


  • 0
    C
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            //reverse the List
            ListNode reverseA = reverseListNode(headA);
            ListNode reverseB = reverseListNode(headB);
            
            ListNode ret = null;
            ListNode iteratorA = reverseA;
            ListNode iteratorB = reverseB;
            while(iteratorA != null && iteratorB != null)
            {
                if(iteratorA.val == iteratorB.val)
                {
                    ret = iteratorA;
                    iteratorA = iteratorA.next;
                    iteratorB = iteratorB.next;
                }
                else
                {
                    break;
                }
            }
            
            headA = reverseListNode(reverseA);
            headB = reverseListNode(reverseB);
            
            return ret;
        }
        
        ListNode reverseListNode(ListNode head)
        {
            ListNode ret = null;
            ListNode tmp = null;
            
            while(head != null)
            {
                tmp = head;
                head = head.next;
                tmp.next = ret;
                ret = tmp;
            }
            
            return ret;
        }
    }

Log in to reply
 

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