参考回答:
判断是否有环以及环节点
public class Solution {ListNode EntryNodeOfLoop(ListNode h){if(h == null || h.next == null)return null;ListNode slow = h;ListNode fast = h;while(fast != null && fast.next != null ){slow = slow.next;fast = fast.next.next;if(slow == fast){ListNode p=h;ListNode q=slow;//相当于让q指向了m1while(p != q){ p = p.next; q = q.next; } if(p == q) return q; } } return null;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)