Middle of the Linked List
找链表中间的点. 用快慢指针
找链表中间的点. 用快慢指针
当年微软面试题: 少了两个= null, 结果木offer public static ListNode sort(ListNode head){ if (head == null) return head; ListNode pivot = new ListNode(head.val); head = head.next; // remove pivot ListNode lessDummy = new ListNode(0); ListNode moreDummy = new ListNode(0); ListNode lessP = lessDummy; ListNode moreP = moreDummy; ListNode p = head; while (p != null){ if (p.val < […]
public boolean hasCycle(ListNode head) { // write your code here if(head == null) return false; ListNode fast = head; ListNode slow = head; while(fast.next != null && fast.next.next != null) { slow = slow.next; fast = fast.next.next; if(slow == fast) return true; } return false; }