题目:环形链表 点击跳转
文章目录
- 题目描述
- 题目解答
题目描述
题目解答
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */publicclassSolution{publicListNodedetectCycle(ListNodehead){ListNodefast=head;ListNodeslow=head;while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;if(fast==slow){ListNodep=head;while(p!=slow){p=p.next;slow=slow.next;}returnp;}}returnnull;}}