• 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

    补充一个python的实现:

     1 class Solution(object):
     2     def getIntersectionNode(self, headA, headB):
     3         """
     4         :type head1, head1: ListNode
     5         :rtype: ListNode
     6         """
     7         tempA = headA
     8         tempB = headB
     9         while tempA != tempB:
    10             if tempA == None:
    11                 tempA = headB
    12             else:
    13                 tempA = tempA.next
    14             if tempB == None:
    15                 tempB = headA
    16             else:
    17                 tempB = tempB.next
    18         return tempA
  • 相关阅读:
    javascript实战演练,制作新按钮,‘新窗口打开网站’,点击打开新窗
    P1332 血色先锋队
    P4643 [国家集训队]阿狸和桃子的游戏
    T149876 公约数
    P1462 通往奥格瑞玛的道路
    P1083 借教室
    Tribles UVA
    Fence Repair POJ
    Crossing Rivers
    关于一轮
  • 原文地址:https://www.cnblogs.com/asenyang/p/6762038.html
Copyright © 2020-2023  润新知