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;
}
}
Reverse Second Half of a Linked List
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.