题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/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 cur = head;
ListNode last = null;
ListNode next = cur.next;
while (cur != null) {
next = cur.next;
cur.next = last;
last = cur;
cur = next;
}
return last;
}
}
查看9道真题和解析
