题目描述:输入两个链表,找出它们的第一个公共结点。
import Foundation
class For37Solution {
func findFirstCommonNode(_ head1: ListNode?,_ head2: ListNode?) -> ListNode {
var pHead = head1
var qHead = head2
var pLen = 0
var qLen = 0
while pHead?.next != nil {
pHead = pHead?.next
pLen += 1
}
while qHead?.next != nil {
qHead = qHead?.next
qLen += 1
}
var pHead1 = head1
var qHead2 = head2
var len = pLen - qLen
if len > 0 {
while len >= 0 {
pHead1 = pHead1?.next
len -= 1
}
}else {
len = -len
while len >= 0 {
qHead2 = qHead2?.next
len -= 1
}
}
while pHead1?.next != nil && qHead2?.next != nil {
if pHead1 === qHead2 {
return pHead1!
}
pHead1 = pHead1?.next
qHead2 = qHead2?.next
}
//没有公共结点
return ListNode(0)
}
}
算法思想:先算出两个链表的长度,假如差为K,长的链表先走K步,然后两个指针一起走,如果相遇就是第一个公共节点。如果没有相遇就没有公共节点。
github地址:https://github.com/cubegao/LeetCode