leetcode刷题(单链表)1—反转链表

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 next = head.next;
        head.next = null;
        while(next != null){
            ListNode next2 = next.next;
            next.next = head;
            head = next;
            next = next2;
        }
        return head;
    }
}

你可能感兴趣的:(学习笔记,链表,java)