LeetCode - 160. Intersection of Two Linked Lists

来源:互联网 发布:mac中如何新建文件夹 编辑:程序博客网 时间:2024/06/05 00:15


代码如下:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { *         val = x; *         next = null; *     } * } */public class Solution {    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {        if(headA == null || headB == null) return null;                ListNode a = headA;        ListNode b = headB;                while(a != b){            a = (a == null)? headB : a.next;            b = (b == null)? headA : b.next;        }                return a;    }}


知识点:

1. 注意这种traverse到end然后转换linked list的方法

0 0
原创粉丝点击