LinkedList: Useful

1. Solution快慢指针找中点时

(1)当结点个数是偶数个,slow停在前半部的最后;奇数停在mid

ListNode slow = head;
    ListNode fast = head;
    while(fast.next != null && fast.next.next != null) { 
        slow = slow.next;
        fast = fast.next.next;
    }

(2)当结点个数是偶数个,slow停在后半部的开始

ListNode slow = head;
    ListNode fast = head;
    while(fast != null && fast.next != null) { 
        slow = slow.next;
        fast = fast.next.next;
    }
2. Reverse linkedlist

http://www.jianshu.com/p/56353df45769

3. Merge two sorted list

http://www.jianshu.com/p/ddad4576e950

3. LinkedList Traversal
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()) {
    System.out.println(listIterator.next());
}

你可能感兴趣的:(LinkedList: Useful)