JZ15 反转链表
反转链表
http://www.nowcoder.com/questionTerminal/75e878df47f24fdc9dc3e400ec6058ca
输入一个链表,反转链表后,输出新链表的表头。
别无他法 老老实实用上一堆指针
(或者直接记录值,更换值,很无聊,略)
/* public class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; } }*/ public class Solution { public ListNode ReverseList(ListNode head) { if(head==null||head.next==null) return head; ListNode prev=null; ListNode curr=head; ListNode next=head.next; while(curr.next!=null){ curr.next=prev; prev=curr; curr=next; next=next.next; } curr.next=prev; return curr; } }