1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class ReverseLinkedList {
public ListNode reverseList(ListNode head) { if (head == null || head.next == null) { return head; } ListNode preHeader = head; ListNode cursor = head.next; preHeader.next = null;
ListNode tmp; while (cursor != null) { tmp = cursor.next; cursor.next = preHeader; preHeader = cursor; cursor = tmp; }
return preHeader; }
}
|