I was working on this problem and was running into time limit exceeded issues when simply running through the list in O(n) time and it took about 100ms using C# and 0ms with Java. Even simply returning head straight from the function takes about 80ms in C#! The time constraints really should be adjusted for C# if this is due to a technical limitation. The code is below.
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode Partition(ListNode head, int x) {
var current = head;
while(current != null){
current = current.next;
}
return head;
}
}