1.查找链表中点,使用两个指针
ListNode searchMidNode(ListNode head) {
ListNode slow = head,
fast = head;
while(fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
if(fast) {
return slow;
}
if(fast.next) {
return slow.next;
}
}
2.查找链表是否有环,使用两个指针
ListNode isCircle(ListNode head) {
ListNode slow = head,
fast = head;
while(fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
if(slow == fast) {
return true;
}
return false;
}
3.有环链表的长度
void circleLen(LinkNode head) {
ListNode slow = head,
fast = head;
int len = 1;
while(fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
len++;
if(slow == fast) {
break;
}
}
}