voiddectectAndRemoveLoop(Node node){// if list is empty or has only one nodeif (node ==null){return; }Node slow = node, fast = node;// search for loopwhile (fast !=null&&fast.next!=null){ slow =slow.next; fast =fast.next.next;if (fast == slow){// search connected node and remove the loop slow = node;while(slow.next!=fast.next){ slow =slow.next; fast =fast.next; }fast.next=null;break; } }}
287. Find the Duplicate Number
LinkedList Cycle index: 0, 1, 2, 3, 4, 5, 6 value: 2, 6, 4, 1, 3, 1, 5 It value can be index, it will become a cycled linkedlist 0 - > 2 - > 4 -> 3 -> 1 -> 6 -> 5-> [1- >6-> 5 ->1 链表环]
Given a linked list, rotate the list to the right by k places, where k is non-negative.
1 create dummy node, a slow and a fast pointer == dummy node 2 get length, fast node move to last 3 slow node move to break index 4 rotate lsit
publicListNoderotateRight(ListNode head,int k) {if (head ==null||head.next==null){return head; }ListNode dummy =newListNode(0);dummy.next= head;ListNode slow = dummy, fast = dummy;int i;for (i =0; fast.next!=null; i++){ // get length & fast move to last index fast =fast.next; }for (int j =0; j < i - k%i; j++){ // slow move to break index slow =slow.next; }fast.next=dummy.next; // rotate listdummy.next=slow.next;slow.next=null;returndummy.next;}
138 Copy List with random pointer
HashMap[oldNode, newNode]
first loop create newNode
Second loop update next and random pointers
Space O(n)
defcopyRandomList(self,head:'Node') ->'Node':ifnot head:returnNone record ={} cur = head while cur: record[cur]=Node(cur.val) cur = cur.next cur = headwhile cur: record[cur].next = record[cur.next]if cur.next elseNone record[cur].random = record[cur.random]if cur.random elseNone cur = cur.nextreturn record[head]
Space O(1) 1 -> 1' -> 2 -> 2' -> 3 -> 3' -> null
1. Copy new nodes after old nodes (including label, next and random)
2. Update random pointers
3. Split the linkedList
defcopyRandomList(self,head:'Node') ->'Node':ifnot head:returnNone# create new node cur = headwhile cur: node =Node(cur.val) node.next = cur.next cur.next = node cur = cur.next.next# link new node next and random pointer cur = headwhile cur: cur.next.random = cur.random.next if cur.random elseNone cur = cur.next.next# Break new and old nodes dummy = new_cur =Node(0) cur = headwhile cur:# new_cur 在 cur 之前 new_cur.next = cur.next new_cur = new_cur.next cur.next = cur.next.next cur = cur.nextreturn dummy.next