两个链表的交叉

/**

  • Definition for singly-linked list.
  • public class ListNode {
  • int val;
    
  • ListNode next;
    
  • ListNode(int x) {
    
  •     val = x;
    
  •     next = null;
    
  • }
    
  • }
    /
    public class Solution {
    /
    *
    • @param headA: the first list

    • @param headB: the second list

    • @return: a ListNode
      /
      public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
      if(headANULL||headBNULL) return NULL;
      ListNode
      p=headA;
      stack<ListNode> my1;
      stack<ListNode
      > my2;
      while(p)
      {
      my1.push(p);
      p=p->next;
      }
      p=headB;
      while(p)
      {
      my2.push(p);
      p=p->next;
      }
      while((!my1.empty())&& (!my2.empty()))
      {

       if((my1.top()->val)==(my2.top()->val))
       {
           p=my1.top();
           my1.pop();
           my2.pop();
       }
       else
          break;
      

      }
      return p;
      }
      };

posted @ 2017-08-22 02:16  逸文皓  阅读(121)  评论(0编辑  收藏  举报