首页 > 代码库 > leetcode160
leetcode160
/** * Definition for singly-linked list. * public class ListNode { * public int val; * public ListNode next; * public ListNode(int x) { val = x; } * } */ public class Solution { public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { if (headA == null || headB == null) { return null; } else { var tempA = headA; var tempB = headB; var listA = new List<ListNode>(); var listB = new List<ListNode>(); while (headA != null) { listA.Add(headA); headA = headA.next; } while (headB != null) { listB.Add(headB); headB = headB.next; } listA.Reverse(); listB.Reverse(); if (listA.Count > listB.Count) { for (int i = 0; i < listB.Count; i++) { if (listA[i].val != listB[i].val) { if (i > 0) { tempB = listB[i - 1]; } else { tempB = null; } break; } else { if (i > 0) { tempB = listB[i]; } } } return tempB; } else { for (int i = 0; i < listA.Count; i++) { if (listA[i].val != listB[i].val) { if (i > 0) { tempA = listA[i - 1]; } else { tempA = null; } break; } else { if (i > 0) { tempA = listA[i]; } } } return tempA; } } } }
https://leetcode.com/problems/intersection-of-two-linked-lists/#/description
leetcode160
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。