LintCode: Convert Linked List to Array List

Convert Linked List to Array List

Similar Problems:


Convert a linked list to an array list.

Given 1->2->3->null, return [1,2,3].


Credits To:

Leave me comments, if you have better ways to solve.

  • Solution:
// Blog link:
// Basic Ideas: One pass
// Complexity: Time O(n), Space O(n)
 * Definition for singly-linked list.
 * type ListNode struct {
 *     Val int
 *     Next *ListNode
 * }

 * @param head: the head of linked list.
 * @return: An integer list
func toArrayList (head *ListNode) []int {
    res := []int{}
    p := head
    for p != nil {
        res = append(res, p.Val)
        p = p.Next
    return res

Share It, If You Like It.

Leave a Reply

Your email address will not be published.