206Reverse Linked List

来源:互联网 发布:五星大饭店知乎 编辑:程序博客网 时间:2024/04/23 18:05
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
if(head==null) return null;
ListNode pre = head, cur = head.next,nxt = null;
pre.next = null;
while(cur != null){
nxt = cur.next;
cur.next = pre;
pre = cur;
cur = nxt;
}
        return pre;
    }
}
0 0
原创粉丝点击