上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
}
};
golang 解法, 执行用时: 36 ms, 内存消耗: 7 MB, 提交时间: 2022-05-26 15:42:02
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
h1, h2 := headA, headB
for h1 != h2 {
if h1 != nil {
h1 = h1.Next
} else {
h1 = headB
}
if h2 != nil {
h2 = h2.Next
} else {
h2 = headA
}
}
return h1
}