LeetCode160 相交链表
创始人
2024-11-20 13:35:28
0

前言

题目:160. 相交链表
文档:代码随想录——链表相交
编程语言: C++
解题状态: 没思路…

思路

依旧是双指针法,很巧妙的方法,有点想不出来。

代码

先将两个链表末端对齐,然后两个指针齐头并进,容易判断出是否相交。

/**  * 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) {         ListNode* curA = headA;         ListNode* curB = headB;          int lenA = 0;         int lenB = 0;          while (curA) {             ++lenA;             curA = curA -> next;         }          while (curB) {             ++lenB;             curB = curB -> next;         }          curA = headA;         curB = headB;          if (lenB > lenA) {             swap(lenA, lenB);             swap(curA, curB);         }          int gap = lenA - lenB;          while (gap--) {             curA = curA -> next;         }          while (curA) {             if (curA == curB) {                 return curA;             }             curA = curA -> next;             curB = curB -> next;         }          return NULL;     } }; 
  • 时间复杂度: O ( m + n ) O(m + n) O(m+n)
  • 空间复杂度: O ( 1 ) O(1) O(1)

相关内容

热门资讯

脚本解密!wpk透视辅助方法,... 脚本解密!wpk透视辅助方法,有哪些免费的wpk作必弊码(透视)真是是真的app(哔哩哔哩);1、上...
脚本解谜!hhpoker软件安... 脚本解谜!hhpoker软件安装包,hhpoker视频巡查真的假的(透视)一直真的是有插件(哔哩哔哩...
脚本分享!wpk显示有作必弊,... 脚本分享!wpk显示有作必弊,wpk俱乐部辅助器(透视)总是真的有软件(哔哩哔哩)1、让任何用户在无...
透视关于!wepoker怎么拿... 透视关于!wepoker怎么拿到好牌,wepokerplus辅助(透视)确实真的是有app(哔哩哔哩...
透视详情!hhpoker怎么破... 透视详情!hhpoker怎么破解,hhpoker底牌透视脚本(透视)总是真的有技巧(哔哩哔哩)1、实...
脚本透视!aapoker脚本,... 脚本透视!aapoker脚本,aapoker如何设置胜率(透视)总是是有技巧(哔哩哔哩)小薇(辅助器...
脚本详情!德普之星辅助工具如何... 脚本详情!德普之星辅助工具如何设置,德普之星辅助工具如何打开(透视)总是真的是有脚本(哔哩哔哩)脚本...
透视推荐!wpk官网下载链接,... 透视推荐!wpk官网下载链接,wpk透视辅助(透视)都是是真的教程(哔哩哔哩)1、超多福利:超高返利...
脚本详情!wpk辅助器,wpk... 脚本详情!wpk辅助器,wpk德州局怎么透视(透视)好像是真的软件(哔哩哔哩)1、德州局怎么透视破解...
透视分享!wejoker透视方... 透视分享!wejoker透视方法,wepoker黑侠辅助器(透视)都是真的有脚本(哔哩哔哩)1、用户...