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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
| package InterviewCoding
import ( "testing" )
func TestIntersection(t *testing.T) { l1, l2 := constructTwoLinkedListWithoutCycleAndNotIntersection() if Intersection(l1, l2) != nil { t.Fatalf("expect no intersection node.") }
l1, l2 = constructTwoLinkedListWithoutCycleButIntersection() if Intersection(l1, l2) == nil { t.Log(Intersection(l1, l2)) t.Fatalf("expect to have intersection node.") }
l1, l2 = constructTwoLinkedListButOneHasCycle() if Intersection(l1, l2) != nil { t.Fatalf("expect no intersection node.") } l1, l2 = constructTwoLinkedBothHasOwnCycle() if Intersection(l1, l2) != nil { t.Fatalf("expect no intersection node.") }
l1, l2 = constructTwoLinkedListBothHasCycleAndIntersection() if Intersection(l1, l2) == nil { t.Fatalf("expect to have intersection node.") } l1, l2 = constructTwoLinkedListBothHasCycleAndIntersectionII() if Intersection(l1, l2) == nil { t.Fatalf("expect to have intersection node.") } }
func constructTwoLinkedListWithoutCycleButIntersection() (l1, l2 *LinkedListNode) { node := &LinkedListNode{Val: 5} l1, l2 = &LinkedListNode{Val: 3}, &LinkedListNode{Val: 4, Next: &LinkedListNode{Val: 6, Next: node}} l1.Next, l2.Next = node, node return l1, l2 }
func constructTwoLinkedListWithoutCycleAndNotIntersection() (l1, l2 *LinkedListNode) { l1, l2 = &LinkedListNode{Val: 3, Next: &LinkedListNode{Val: 4}}, &LinkedListNode{Val: 5, Next: &LinkedListNode{Val: 6}} return l1, l2 }
func constructTwoLinkedListButOneHasCycle() (l1, l2 *LinkedListNode) { node, node2 := &LinkedListNode{Val: 2}, &LinkedListNode{Val: 3} node.Next, node2.Next = node2, node l1, l2 = &LinkedListNode{Val: 1, Next: node}, &LinkedListNode{Val: 3} return l1, l2 }
func constructTwoLinkedBothHasOwnCycle() (l1, l2 *LinkedListNode) { node, node2 := &LinkedListNode{Val: 2}, &LinkedListNode{Val: 3} node.Next, node2.Next = node2, node node3, node4 := &LinkedListNode{Val: 4}, &LinkedListNode{Val: 5} node3.Next, node4.Next = node4, node3 l1, l2 = &LinkedListNode{Val: 1, Next: node}, &LinkedListNode{Val: 3, Next: node3} return l1, l2 }
func constructTwoLinkedListBothHasCycleAndIntersection() (l1, l2 *LinkedListNode) { node, node2 := &LinkedListNode{Val: 4}, &LinkedListNode{Val: 5} node.Next, node2.Next = node2, node node3 := &LinkedListNode{Val: 6, Next: node} l1, l2 = &LinkedListNode{Val: 1, Next: node3}, &LinkedListNode{Val: 3, Next: node3} return l1, l2 }
func constructTwoLinkedListBothHasCycleAndIntersectionII() (l1, l2 *LinkedListNode) { node, node2 := &LinkedListNode{Val: 4}, &LinkedListNode{Val: 5} node.Next, node2.Next = node2, node l1, l2 = &LinkedListNode{Val: 1, Next: node}, &LinkedListNode{Val: 3, Next: &LinkedListNode{Val: 6, Next: node2}} return l1, l2 }
|