题目描述
输入一个链表,反转链表后,输出新链表的表头。
输入:
{1,2,3}
输出:
{3,2,1}
代码:
public class Solution {
public ListNode ReverseList(ListNode head) {
if(head==null || head.next==null){
return head;
}
ListNode cur = head;
ListNode next = null;
ListNode pre = null;
while(cur!=null){
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}