ISquared
ISquared

Reputation: 472

How can a LinkedList Node head be an array? (leetcode-defined inputs confusing)

There's a question I'm trying to solve on Leetcode and as dumb as it is, I don't understand the inputs in order to test it and I don't see anything in the comments/discussion about it.

This question isn't about how to solve the challenge itself. I don't understand how, using the example test cases in Leetcode, I an meant to run it in Visual Studio. I do my challenges in a VS solution first, where I can debug and check with various use cases. Normally it's pretty straightforward what the input is, now it just doesn't make sense.

It's just about checking if a LinkedList is a palindrome or not:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int val=0, ListNode next=null) {
 *         this.val = val;
 *         this.next = next;
 *     }
 * }
 */
public class Solution {
    public bool IsPalindrome(ListNode head) {
        
    }
}

The confusing part is in the use cases in Leetcode, They pass an array of integers as a parameter (supposed to be head):

Input: head = [1,2,2,1]
Output: true

Input: head = [1,2]
Output: false

Obviously, the array must be the LinkedList itself, the individual node values. I thought I'd just build a LinkedList from the integers in the array and carry on. However, as you see, the parameter you pass as input is (apparently?) an array itself, and this doesn't work with the ListNode structure, which clearly should accept a single integer as a value for the head node.

I even reluctantly checked the beginning of the solutions in order to understand what they Hell I have to do, but it doesn't help me with the input at all ..

Sorry if it's out of scope, just a bit difficult to progress this way.

Upvotes: 0

Views: 394

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186813

As I can see, you want to create a more convenient collection (say, List<int>; array is a worse option since we don't know its Length) from the given linked list:

public class Solution {
    public bool IsPalindrome(ListNode head) {
        if (head is null)
            return false; // or true, consult the problem please
   
        List<int> list = new List<int>();       

        for (ListNode node = head; node != null; node = node.next)
            list.Add(node.val); 
    }
}

Having List<int> you can easily check if it's palindrome or not:

public class Solution {
    public bool IsPalindrome(ListNode head) {
        if (head is null)
            return false; // or true, consult the problem please
   
        List<int> list = new List<int>();       

        for (ListNode node = head; node != null; node = node.next)
            list.Add(node.val);

        for (int i = 0; i < list.Count / 2; ++i)
            if (list[i] != list[list.Count - 1 - i])
                return false;

        return true; 
    }
}

Upvotes: 1

vencelbajnok
vencelbajnok

Reputation: 9

S.Shakeri is right. I would only add that, based on the code you shared, the task is mainly to complete the IsPalindrome function, which for you not necessarily need to deal with the integer array, rather you should walk through the linked list and check some way, whether it has its nodes ordered in a palindrome, or not.

For walking through the list, the first answer and the definition for the ListNode class should be enough help.

And for determining whether the list is palindrome, maybe you could think about how would you do that on paper in a way, that at once you have access only to one and the next element of the list. After that you only need to code it down and succeed.

Please ask your questions, whether something isn't clear, or you got stuck.


I'm not planning to tell you the actual solution in code, as this is a task from Leetcode, but I'm trying to show you the way, you can manage it on your own.

Upvotes: 1

Related Questions