zoukankan      html  css  js  c++  java
  • LeetCode 160. Intersection of Two Linked Lists

    这个题目较容易,我的想法是先比较两个链表的长度,我们要找到的交叉点是不可能为长的链表的前面超长的部分的,

    所以要比较的就是两者之间较短的部分

    Write a program to find the node at which the intersection of two singly linked lists begins.

    For example, the following two linked lists:

    A:          a1 → a2
                       ↘
                         c1 → c2 → c3
                       ↗            
    B:     b1 → b2 → b3
    

    begin to intersect at node c1.

    Notes:

    • If the two linked lists have no intersection at all, return null.
    • The linked lists must retain their original structure after the function returns.
    • You may assume there are no cycles anywhere in the entire linked structure.
    • Your code should preferably run in O(n) time and use only O(1) memory
      /**
       * Definition for singly-linked list.
       * struct ListNode {
       *     int val;
       *     ListNode *next;
       *     ListNode(int x) : val(x), next(NULL) {}
       * };
       */
      class Solution {
      public:
          ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
              size_t lengthA=0, lengthB=0,minus=0;
              ListNode *pA=headA, *pB=headB,*IntersectionNode=NULL;
              while (pA != NULL)
              {
                  ++lengthA;
                  pA = pA->next;
              }
              while (pB != NULL)
              {
                  ++lengthB;
                  pB = pB->next;
              }
              if (lengthA > lengthB)
              {
                  pA = headA;
                  pB = headB;
                  minus = lengthA - lengthB;
                  while (minus--)
                  {
                      pA = pA->next;
                  }
                  while (pA != NULL&&pB != NULL)
                  {
                      if (pA->val == pB->val&&IntersectionNode==NULL)
                          IntersectionNode = pA;
                      if (pA->val != pB->val)
                          IntersectionNode = NULL;
                      pA = pA->next;
                      pB = pB->next;
                  }
                  return IntersectionNode;
              }
              else
              {
                  pA = headA;
                  pB = headB;
                  minus = lengthB - lengthA;
                  while (minus--)
                  {
                      pB = pB->next;
                  }
                  while (pA != NULL&&pB != NULL)
                  {
                      if (pA->val == pB->val&&IntersectionNode == NULL)
                          IntersectionNode = pA;
                      if (pA->val != pB->val)
                          IntersectionNode = NULL;
                      pA = pA->next;
                      pB = pB->next;
                  }
                  return IntersectionNode;
              }
          }
      };
    •  

  • 相关阅读:
    转 C#接收邮件
    WebApi的windows服务之路
    自动化升级工具之客户端升级工具之windows服务升级操作
    自动化升级工具之客户端升级工具之数据库升级操作
    GoJs实现流程管理图
    MvcForum作者称该项目进入缓慢更新
    生鲜蔬菜生产称重管理系统
    MvcForum中文版+PostgreSql源码下载
    Asp.Net MVC开源论坛中文版
    HTML5+Activex+Singr+ABP+MongoDB
  • 原文地址:https://www.cnblogs.com/csudanli/p/5746886.html
Copyright © 2011-2022 走看看