输入一个链表,反转链表后,输出新链表的表头。
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}*/
public class Solution {
public ListNode ReverseList(ListNode head) {
ListNode res = null;
ListNode cur = head;
ListNode pre = null;
while(cur!=null)
{
ListNode next = cur.next;
if(next==null)
res = cur;
cur.next = pre;
pre = cur;
cur = next;
}
return res;
}
}