|
| 1 | +package middleofthelinkedlist |
| 2 | + |
| 3 | +import "testing" |
| 4 | + |
| 5 | +func TestMiddleNode_SingleNode(t *testing.T) { |
| 6 | + head := &ListNode{Val: 1} |
| 7 | + result := middleNode(head) |
| 8 | + |
| 9 | + if result != head { |
| 10 | + t.Error("Expected single node to be returned as middle") |
| 11 | + } |
| 12 | + if result.Val != 1 { |
| 13 | + t.Errorf("Expected value 1, got %d", result.Val) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func TestMiddleNode_TwoNodes(t *testing.T) { |
| 18 | + head := &ListNode{Val: 1} |
| 19 | + second := &ListNode{Val: 2} |
| 20 | + head.Next = second |
| 21 | + |
| 22 | + result := middleNode(head) |
| 23 | + |
| 24 | + if result != second { |
| 25 | + t.Error("Expected second node to be returned as middle") |
| 26 | + } |
| 27 | + if result.Val != 2 { |
| 28 | + t.Errorf("Expected value 2, got %d", result.Val) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestMiddleNode_ThreeNodes(t *testing.T) { |
| 33 | + head := &ListNode{Val: 1} |
| 34 | + head.Next = &ListNode{Val: 2} |
| 35 | + head.Next.Next = &ListNode{Val: 3} |
| 36 | + |
| 37 | + result := middleNode(head) |
| 38 | + |
| 39 | + if result.Val != 2 { |
| 40 | + t.Errorf("Expected middle value 2, got %d", result.Val) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func TestMiddleNode_FourNodes(t *testing.T) { |
| 45 | + head := &ListNode{Val: 1} |
| 46 | + head.Next = &ListNode{Val: 2} |
| 47 | + head.Next.Next = &ListNode{Val: 3} |
| 48 | + head.Next.Next.Next = &ListNode{Val: 4} |
| 49 | + |
| 50 | + result := middleNode(head) |
| 51 | + |
| 52 | + if result.Val != 3 { |
| 53 | + t.Errorf("Expected second middle value 3, got %d", result.Val) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestMiddleNode_Example1(t *testing.T) { |
| 58 | + head := &ListNode{Val: 1} |
| 59 | + head.Next = &ListNode{Val: 2} |
| 60 | + head.Next.Next = &ListNode{Val: 3} |
| 61 | + head.Next.Next.Next = &ListNode{Val: 4} |
| 62 | + head.Next.Next.Next.Next = &ListNode{Val: 5} |
| 63 | + |
| 64 | + result := middleNode(head) |
| 65 | + |
| 66 | + if result.Val != 3 { |
| 67 | + t.Errorf("Expected middle value 3, got %d", result.Val) |
| 68 | + } |
| 69 | + |
| 70 | + values := []int{} |
| 71 | + for curr := result; curr != nil; curr = curr.Next { |
| 72 | + values = append(values, curr.Val) |
| 73 | + } |
| 74 | + expected := []int{3, 4, 5} |
| 75 | + if len(values) != len(expected) { |
| 76 | + t.Errorf("Expected remaining list length %d, got %d", len(expected), len(values)) |
| 77 | + } |
| 78 | + for i := range expected { |
| 79 | + if values[i] != expected[i] { |
| 80 | + t.Errorf("At position %d: expected %d, got %d", i, expected[i], values[i]) |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func TestMiddleNode_Example2(t *testing.T) { |
| 86 | + head := &ListNode{Val: 1} |
| 87 | + head.Next = &ListNode{Val: 2} |
| 88 | + head.Next.Next = &ListNode{Val: 3} |
| 89 | + head.Next.Next.Next = &ListNode{Val: 4} |
| 90 | + head.Next.Next.Next.Next = &ListNode{Val: 5} |
| 91 | + head.Next.Next.Next.Next.Next = &ListNode{Val: 6} |
| 92 | + |
| 93 | + result := middleNode(head) |
| 94 | + |
| 95 | + if result.Val != 4 { |
| 96 | + t.Errorf("Expected second middle value 4, got %d", result.Val) |
| 97 | + } |
| 98 | + |
| 99 | + values := []int{} |
| 100 | + for curr := result; curr != nil; curr = curr.Next { |
| 101 | + values = append(values, curr.Val) |
| 102 | + } |
| 103 | + expected := []int{4, 5, 6} |
| 104 | + if len(values) != len(expected) { |
| 105 | + t.Errorf("Expected remaining list length %d, got %d", len(expected), len(values)) |
| 106 | + } |
| 107 | + for i := range expected { |
| 108 | + if values[i] != expected[i] { |
| 109 | + t.Errorf("At position %d: expected %d, got %d", i, expected[i], values[i]) |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestMiddleNode_SevenNodes(t *testing.T) { |
| 115 | + head := &ListNode{Val: 1} |
| 116 | + curr := head |
| 117 | + for i := 2; i <= 7; i++ { |
| 118 | + curr.Next = &ListNode{Val: i} |
| 119 | + curr = curr.Next |
| 120 | + } |
| 121 | + |
| 122 | + result := middleNode(head) |
| 123 | + |
| 124 | + if result.Val != 4 { |
| 125 | + t.Errorf("Expected middle value 4, got %d", result.Val) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestMiddleNode_EightNodes(t *testing.T) { |
| 130 | + head := &ListNode{Val: 1} |
| 131 | + curr := head |
| 132 | + for i := 2; i <= 8; i++ { |
| 133 | + curr.Next = &ListNode{Val: i} |
| 134 | + curr = curr.Next |
| 135 | + } |
| 136 | + |
| 137 | + result := middleNode(head) |
| 138 | + |
| 139 | + if result.Val != 5 { |
| 140 | + t.Errorf("Expected second middle value 5, got %d", result.Val) |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func TestMiddleNode_MaxConstraint(t *testing.T) { |
| 145 | + head := &ListNode{Val: 100} |
| 146 | + curr := head |
| 147 | + for i := 99; i >= 1; i-- { |
| 148 | + curr.Next = &ListNode{Val: i} |
| 149 | + curr = curr.Next |
| 150 | + } |
| 151 | + |
| 152 | + result := middleNode(head) |
| 153 | + |
| 154 | + if result == nil { |
| 155 | + t.Error("Expected non-nil result for 100 nodes") |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestMiddleNode_TenNodes(t *testing.T) { |
| 160 | + head := &ListNode{Val: 10} |
| 161 | + curr := head |
| 162 | + for i := 20; i <= 100; i += 10 { |
| 163 | + curr.Next = &ListNode{Val: i} |
| 164 | + curr = curr.Next |
| 165 | + } |
| 166 | + |
| 167 | + result := middleNode(head) |
| 168 | + |
| 169 | + if result.Val != 60 { |
| 170 | + t.Errorf("Expected second middle value 60, got %d", result.Val) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestMiddleNode_NineNodes(t *testing.T) { |
| 175 | + head := &ListNode{Val: 1} |
| 176 | + curr := head |
| 177 | + for i := 2; i <= 9; i++ { |
| 178 | + curr.Next = &ListNode{Val: i * 10} |
| 179 | + curr = curr.Next |
| 180 | + } |
| 181 | + |
| 182 | + result := middleNode(head) |
| 183 | + |
| 184 | + if result.Val != 50 { |
| 185 | + t.Errorf("Expected middle value 50, got %d", result.Val) |
| 186 | + } |
| 187 | +} |
0 commit comments