141. Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up :
Can you solve it without using extra space ?
题意:
给定一个链表,判断此链表是否有环,不使用额外内存空间。
思路:
定义一个快指针pre,定义一个慢指针last,遍历给定链表,快指针每次前进两步pre = pre->next->next,慢指针每次前进一步 last = last->next,当快慢指针相与时,说明链表存在环,反之不存在。
1 | /** |