789. Special Linked List

Given the head of Linked List and an integer val, partition the list as a special Linked List.

A special linked list is one in which all nodes with values less than val come before all nodes equal to or greater than val. You have to keep the relative ordering of the nodes within the partition the same as the initial list.

Example 1:

Input: head -> 5 -> 2 -> 4 -> 1 -> 3 -> 4, val = 3

Output: head -> 2 -> 1 -> 5 -> 4 -> 3 -> 4

Explanation: head -> 5 -> 2 -> 4 -> 1 -> 3 -> 4

The underlined nodes are less than val, so they come before others.

Note that the ordering of elements within the group is maintained.

Example 2:

Input: head -> 3 -> 7 -> 2 -> 5 -> 3 -> 1, val = 4

Output: head -> 3 -> 2 -> 3 -> 1 -> 7 -> 5

Explanation: head -> 3 -> 7 -> 2 -> 5 -> 3 -> 1

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= Number of Nodes in the Linked List <= 105
  • -104 <= ListNode.val <= 104
  • -104 <= val <= 104

Fun Facts

0
 
/*
Definition of singly linked list:
struct ListNode
{
int val;
ListNode *next;
ListNode()
{
val = 0;
next = NULL;
}
ListNode(int data1)
{
val = data1;
next = NULL;
}
ListNode(int data1, ListNode *next1)
{
val = data1;
next = next1;
}
};
*/
 
class Solution {
public:
ListNode* partitionList(ListNode* head, int val) {
 
}
};
Test Case

Input:

Nums
Val