141. 环形链表
解题思路
1.通过循环遍历,遍历过程中同时保存已遍历得到的node
2.同时做判断,下一个遍历到的因素,位置与值是否已在之前保存的列表中
3.如果已存在,则返回true,如果遍历到末尾(null),则返回false
解题遇到的问题
无
后续需要总结学习的知识点
无
##解法1
public class Solution {
public boolean hasCycle(ListNode head) {
List<ListNode> valList = new ArrayList<ListNode>();
while (head != null) {
if (valList.contains(head)) {
return true;
} else {
valList.add(head);
head = head.next;
}
}
return false;
}
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
}
网友评论