206.反转链表
原题
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode cur = head;
ListNode pre = null;
while(cur!=null){
ListNode temp = cur.next;//暂存下一个节点
cur.next = pre;//将当前指针指向前一个节点
pre = cur;//暂存当前结点
cur = temp; //当前节点向后移动
}
return pre;
}
}