Leetcode 160. Intersection of Two Linked Lists/CC150 2.7

来源:互联网 发布:js给div加class 编辑:程序博客网 时间:2024/06/05 00:50

https://leetcode.com/problems/intersection-of-two-linked-lists/

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

A:          a1 → a2                   ↘                     c1 → c2 → c3                   ↗            B:     b1 → b2 → b3

begin to intersect at node c1.

Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.
思路:
1 遍历linkedList,得到length和tail;
2 Compare tails(reference 对比,不是value对比),如果不同,直接返回;没有intersection;
3 指针指向每个linked List的开始;
4 较长的linked List,移动两个linked list的长度差;
5 遍历每个linked list, 直到指针相同;

C#代码
ListNode FindIntersection(ListNode list1,ListNode list2)        {            if (list1 == null || list2 == null) return null;            //获取tail和size            Result result1 = GetTailAndSize(list1);            Result result2 = GetTailAndSize(list2);            //如果tail reference不同,就没有intersection            if (result1.tail != result2.tail)                return null;            //定位2个指针到linked list的头部            ListNode shorter = result1.size < result2.size ? list1 : list2;            ListNode longer =  result1.size < result2.size ? list2 : list1;            //长linked list的指针向前移动长度差            longer = GetKthNode(longer, Math.Abs(result1.size-result2.size));            //移动两根指针,直到collision            while (shorter != longer)            {                shorter = shorter.next;                longer = longer.next;            }            //任意返回一个            return longer;        }        Result GetTailAndSize(ListNode list)        {            if (list == null)                return null;            int size = 1;            ListNode current = list;            while (current.next != null)            {                size++;                current = current.next;            }            return new Result(current,size);        }        ListNode GetKthNode(ListNode head,int k)        {            ListNode current = head;            while ( k>0 && current != null )            {                current = current.next;                k--;            }            return current;        }

 public class ListNode    {      public int val;      public ListNode next;      public ListNode(int x) { val = x; }  }    public class Result    {        public ListNode tail;        public int size;        public Result(ListNode _tail,int _size)        {            tail = _tail;            size = _size;        }    }




0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 7岁儿童吃了就吐怎么办 狗狗拉稀呕吐不吃饭只喝水怎么办 宝宝吃坏肚子上吐下泄怎么办 一岁宝宝又吐又拉怎么办 八个月宝宝吃什么吐什么怎么办 九个月宝宝吃什么吐什么怎么办 三岁宝宝吃什么吐什么怎么办 2岁宝宝又拉又吐怎么办 6岁儿子又吐又拉怎么办 2岁宝宝吃了就吐怎么办 宝宝吃饱了就吐吐了在吃怎么办 3岁宝宝吃了就吐怎么办 6岁儿童又吐又拉怎么办 还没满月的宝宝肚子胀气怎么办 婴儿拉屎有粘液像果冻状怎么办 宝宝不爱吃饭怎么办吃就吐出来 19天的宝宝生理性腹泻怎么办 33天的婴儿生理性腹泻怎么办 两个多月宝宝生理性腹泻怎么办 3岁儿童又吐又拉怎么办 宝宝两岁了突然说话有点口吃怎么办 姜文怎么克服口吃 说话结巴怎么办 慢性咽炎嗓子干哑说话费劲怎么办 感冒后嗓子不疼 说话费劲怎么办 小孩子在幼儿园内向老尿裤子怎么办 五岁宝宝总是抽鼻子有黄鼻涕怎么办 2岁宝宝头有大人的那么大怎么办 一岁四个月宝宝不理人不说话怎么办 两岁七个月还不会说话怎么办 2岁3个月宝宝说话结巴怎么办 两周的宝宝突然说话结巴怎么办 宝宝3岁半了说话口吃怎么办? 四十天的宝宝吐奶严重怎么办 宝宝吐奶从鼻子喷出来怎么办 六个多月宝宝吐奶一股酸味怎么办 50天的宝宝吃了就吐怎么办 微信语音群聊超过9人怎么办 四个月宝宝母乳不够不喝奶粉怎么办 四个月的宝宝母乳不够吃怎么办 火山直播十多天了还都是假人怎么办 吉利汽车锁了后屁股灯还亮怎么办