翻转一个链表
/** * Definition for ListNode * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { /** * @param head: n * @return: The new head of reversed linked list. */ public ListNode reverse(ListNode head) { // write your code here if (null == head) { return head; } ListNode pre = head; ListNode cur = head.next; ListNode nxt; while (null != cur) { nxt = cur.next; cur.next = pre; pre = cur; cur = nxt; } // 将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head head.next = null; head = pre; return head; } }