LeetCode-206. 反转链表

105 阅读1分钟

LeetCode-206. 反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null){
            return head;
        }
        ListNode last = head;
        ListNode current=last.next;
        while(current!=null){
            last.next = current.next;
            current.next =head;
            head=current;
            current = last.next;
        }

        return head;
    }
}