Skip to content

Commit 8e0bb26

Browse files
authored
Update linked_list.md
add get mid node and weather has a circle
1 parent 958458d commit 8e0bb26

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

zh-hans/basics_data_structure/linked_list.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,39 @@ class NodeCircle:
218218
else:
219219
return False
220220
```
221+
```c++: is a circle
222+
class List
223+
{
224+
public:
225+
bool iscircle(ListNode* head)
226+
{
227+
if (!head || !head->next) return false;
228+
ListNode *low = head, *fast = high;
229+
while(fast && fast->next)
230+
{
231+
low = low->next;
232+
fast = fast->next->next;
233+
if (low == fast) return true;
234+
}
235+
return false;
236+
}
237+
};
238+
```
239+
240+
```c++
241+
class List
242+
{
243+
public:
244+
ListNode* InLisNodet(ListNode *head)
245+
{
246+
if (!head || !head->next) return head;
247+
ListNode *low = head, *fast = head;
248+
while(fast && fast->next)
249+
{
250+
low = low->next;
251+
fast = fast->next->next;
252+
}
253+
return low;
254+
}
255+
};
256+
```

0 commit comments

Comments
 (0)