力扣记录(二)
Lc26 删除有序数组中的重复项
public static int removeDuplicates(int[] nums) {
int idx= 0;
for(int i=1;i<nums.length-1;i++){
if(nums[i]!=nums[idx]){
// 这里重点是先加idx再赋值 之前写的是先赋值就会错
nums[++idx] = nums[i];
}
}
return idx+1;
}
LC141 循环链表(使用快慢指针)
public boolean hasCycle(ListNode head) {
ListNode slow = head,fast=head;
// 条件用来检测快指针
while(fast!=null && fast.next!=null){
//快慢指针
slow = slow.next;
fast = fast.next.next;
// 如果相遇则表示有环
if(slow==fast){
return true;
}
}
return false;
}