Does my code ugly?


  • 0
    J

    //please give me some advices.
    /**

    • 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) {
      if (headA == null || headB == null){ return null;}
      HashMap<ListNode, Integer> map = new HashMap<>();
      ListNode result = null;
      if(headA == headB) return headA;
      while(headA.next == null && headB.next != null){

              if(headA == headB)  return headB;
              headB = headB.next;
              if(headA == headB)  return headB;
           
       }
       while(headB.next == null && headA.next != null){
           
              if(headA == headB)   return headA;
              headA = headA.next;
              if(headA == headB)   return headA;
          
       }
       
       while(headA.next != null){
           ListNode  temp = headA;
           headA = headA.next;
           map.put(temp, 1);
           if(headA.next == null){
               map.put(headA, 1);
           }
       }
       
       while(headB.next != null){
           ListNode temp = headB;
           headB = headB.next;
           if(map.containsKey(temp)){
               return temp;
           }
           if(map.containsKey(headB)){
               return headB;
           }
       }
       
       return result; 
      

      }
      }


Log in to reply
 

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