Reverse Second Half of Linked List
package ReverseSecondHalfList;
/**
* Created by yang on 1/17/17.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
this.val = x;
}
}
public class Solution {
public static ListNode reverseSecondHalfList(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode pre = slow.next;
ListNode cur = pre.next;
while (cur != null) {
pre.next = cur.next;
cur.next = slow.next;
slow.next = cur;
cur = pre.next;
}
return head;
}
}
Last updated