Reverse Second Half of a Linked List


class ListNode {
	int val;
	ListNode next;
	ListNode(int x) { val = x; }
}

public class ReverseSecondHalfofLinkedList {
	public static ListNode reverseSecondHalf(ListNode head) {
		if(head == null || head.next == null)
			return head;
        ListNode slow = head, fast = slow.next;
        while(fast.next != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode pre = null, cur = slow.next;
        while(cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        slow.next = pre;
        return head;
	}
}

你可能感兴趣的:(Java,List,Two,Pointer,面试,算法,链表,双指针)