题解 | 两个链表的第一个公共结点
两个链表的第一个公共结点
https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46
/**
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
object Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
fun FindFirstCommonNode(pHead1: ListNode?,pHead2: ListNode?): ListNode? {
// write code here
var n1:ListNode? = pHead1
var n2:ListNode? = pHead2
while(n1!=n2){
n1 = if(n1==null) pHead2 else n1.next
n2 = if(n2 == null) pHead1 else n2.next
}
return n1
}
}
字节跳动公司福利 1393人发布
