码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode160

时间:2017-04-25 16:51:53      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:rip   target   cti   lan   int   log   nod   problem   desc   

/**
 * 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

标签:rip   target   cti   lan   int   log   nod   problem   desc   

原文地址:http://www.cnblogs.com/asenyang/p/6762038.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!