題目描述:
反轉一個單鏈表。
輸入:
1->2->3->4->5->NULL
輸出:
5->4->3->2->1->NULL
題意:
題目描述
題解:
直接搞,其中記錄一下即可
代碼:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode pre = null;
ListNode cur = head;
while(cur != null){
ListNode tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
}