leetcode160

Posted AsenYang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章

代码片段:Shell脚本实现重复执行和多进程

leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段

leetcode第一题

LeetCode系列160. 相交链表

Leetcode.1024 视频拼接

LeetCode 160. 相交链表