Middle of Linked List: Helper Method

Jay·2022년 5월 24일
0

Grind 120

목록 보기
17/38


First Thoughts: create helper method to return length of given linked list. In main solution method, calculate middle index by dividing length plus one. Traverse the input linked list and if its "index" is equal to middle index, return head (return type is list node)

My Solution:

class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode reversed = null;
        while (head!=null) {
            reversed = new ListNode(head.val, reversed);
            head = head.next;
        }
        return reversed;
    }
}

0개의 댓글