File tree Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Expand file tree Collapse file tree 1 file changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -121,6 +121,7 @@ assertEqual(xx.getAge(), 110);
121
121
## 函数
122
122
123
123
``` js
124
+ // 判断链表是否有环
124
125
bool hasCycle (ListNode head ){
125
126
if (head == nil){
126
127
return false ;
@@ -152,6 +153,23 @@ bool hasCycle(ListNode head){
152
153
}
153
154
return ret;
154
155
}
156
+
157
+ ListNode l1 = ListNode (1 , nil);
158
+ bool b1 = hasCycle (l1);
159
+ println (b1);
160
+ assertEqual (b1, false );
161
+
162
+ ListNode l4 = ListNode (4 , nil);
163
+ ListNode l3 = ListNode (3 , l4);
164
+ ListNode l2 = ListNode (2 , l3);
165
+ bool b2 = hasCycle (l2);
166
+ println (b2);
167
+ assertEqual (b2, false );
168
+
169
+ l4 .next = l2;
170
+ bool b3 = hasCycle (l2);
171
+ println (b3);
172
+ assertEqual (b3, true );
155
173
```
156
174
157
175
函数声明语法:` typeTypeOrVoid? IDENTIFIER formalParameters ('[' ']')* `
You can’t perform that action at this time.
0 commit comments