/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ funckthToLast(head *ListNode, k int)int { p := head
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ funcdetectCycle(head *ListNode) *ListNode { slow, fast := head, head
for fast != nil && fast.Next != nil { fast = fast.Next.Next slow = slow.Next
if fast == slow { for head != slow { head = head.Next slow = slow.Next }
/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */ funcgetIntersectionNode(headA, headB *ListNode) *ListNode { a, b := headA, headB
for a != b { if a == nil { a = headB } else { a = a.Next }