AnyPS5远程串流实战:低延迟与画质调优指南
2026/10/15 6:38:09
环形链表
fast和慢指针slow都指向头节点head。true;若快指针走到末尾(fast或fast.next为null),说明链表无环,返回false。publicclassSolution{publicbooleanhasCycle(ListNodehead){ListNodefast=head;ListNodeslow=head;while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;if(slow==fast){returntrue;}}returnfalse;}}环形链表II
publicclassSolution{publicListNodedetectCycle(ListNodehead){ListNodefast=head;ListNodeslow=head;// 判断是否有环while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;if(slow==fast){break;}}// 无环情况if(fast==null||fast.next==null){returnnull;}// 找入环第一个节点slow=head;while(slow!=fast){fast=fast.next;slow=slow.next;}returnslow;}}链表的回文结构
用快慢指针找到链表的中间节点。
反转中间节点之后的链表部分。
从原来链表的头和尾依次比较节点值是否相等,若都相等则为回文链表。
publicclassSolution{publicbooleanchkPalindrome(ListNodeA){if(A==null||A.next==null){returntrue;}ListNodeslow=A;ListNodefast=A;// 找中间节点while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;}// 反转后半部分链表ListNodecur=slow.next;ListNodecurNext;while(cur!=null){curNext=cur.next;cur.next=slow;slow=cur;cur=curNext;}// 比较原链表前半部分和反转后的后半部分ListNodefirst=A;while(first!=slow){if(first.val!=slow.val){returnfalse;}// 处理偶数长度链表的终止情况if(first.next==slow){returntrue;}first=first.next;slow=slow.next;}returntrue;}}注意:
反转后半个列表后slow就在原列表尾部,所以由first从前往后,slow从后往前遍历列表
first和slow互换的情况。当first.next==slow即遍历完成相交链表
lenA和lenB。publicclassSolution{publicListNodegetIntersectionNode(ListNodeheadA,ListNodeheadB){if(headA==null||headB==null){returnnull;}ListNodepl=headA;ListNodeps=headB;intlenA=0;intlenB=0;while(pl!=null){lenA++;pl=pl.next;}while(ps!=null){lenB++;ps=ps.next;}intlen=lenA-lenB;pl=headA;ps=headB;if(len<0){len=-len;pl=headB;ps=headA;}for(inti=0;i<len;i++){pl=pl.next;}while(ps!=pl){ps=ps.next;pl=pl.next;}returnpl;}}