* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public void reorderList(ListNode head) {
if(head == null){
return;
}
ListNode slow = head;
ListNode fast = head;
while(fast.next != null && fast.next.next != null){
slow = slow.next;
fast = fast.next.next;
}
ListNode newHead= slow.next;
slow.next = null;
newHead = reverseNode(newHead);
while(newHead != null){
ListNode temp = newHead.next;
newHead.next = head.next;
head.next = newHead;
head = newHead.next;
newHead = temp;
}
}
private ListNode reverseNode(ListNode tempHead){
ListNode pre = null;
ListNode currentNode = tempHead;
while(currentNode != null){
ListNode next = currentNode.next;
currentNode.next = pre;
pre = currentNode;
currentNode = next;
}
return pre;
}
}