Cosmoteer Modding Documentation

Popular Posts

Sunday, 29 January 2023

LeetCode: 206. Reverse Linked List

 


Given the head of a singly linked list, reverse the list, and return the reversed list.


class Solution {

    public ListNode reverseList(ListNode head) {
        if (head == null) 
            return null;

        ListNode cur = head, pre = null; 

        while (cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp; 
        }
        return pre; 
    }
}

No comments:

Post a Comment