leetcode-160 相交链表

160. 相交链表

难度: 简单




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getIntersectionNode(headA, headB *ListNode) *ListNode {
// boundary check
if headA == nil || headB == nil {
return nil
}

a := headA
b := headB

// if a & b have different len, then we will stop the loop after second iteration
for a != b {
// for the end of first iteration, we just reset the pointer to the head of another linkedlist
if a == nil {
a = headB
} else {
a = a.Next
}

if b == nil {
b = headA
} else {
b = b.Next
}
fmt.Printf("a = %v b = %v\n", a, b)
}
return a
}

https://www.cnblogs.com/yxh-amysear/p/9608796.html

http://c.biancheng.net/view/8250.html

https://zhuanlan.zhihu.com/p/83138917


更直接的方式

1
2
3
4
func findKthLargest1(nums []int, k int) int {
sort.Ints(nums)
return nums[len(nums)-k]
}

文章目录