Convert Array List to Linked List

题目:

Convert an array list to a linked list.

分析:

解法:

public class Solution {
    /**
     * @param nums an integer array list
     * @return the first node of linked list
     */
    public ListNode toLinkedList(ArrayList<Integer> nums) {  
        // Write your code here
        ListNode dummy = new ListNode(-1);
        ListNode curr = dummy;
        for (int i = 0; i < nums.size(); i++) {
            curr.next = new ListNode(nums.get(i) );
            curr = curr.next;
        }
        return dummy.next;
    }
}

results matching ""

    No results matching ""