141.环形链表
这题不会,第一次用快慢指针
public class Solution {
public boolean hasCycle(ListNode head) {
ListNode fast=new ListNode(),slow=new ListNode();
fast=head;
slow=head;
if(head==null)
return false;
while(fast.next!=null&&slow.next!=null){
fast=fast.next;
if(fast.next!=null)
fast=fast.next;
if(fast==slow)
return true;
slow=slow.next;
}
return false;
}
}21.合并两个有序链表
这题是当时学长的考核,双指针,确实也不是太难。
class Solution {
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
ListNode res=new ListNode();
ListNode p1=list1;
ListNode p2=list2;
ListNode p=res;
while(p1!=null&&p2!=null)
{
if(p1.val
Original: https://blog.csdn.net/m0_65280246/article/details/127814385
Author: 紫微帝星
Title: 力扣(141.21)补9.1
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/653605/
转载文章受原作者版权保护。转载请注明原作者出处!