Question: Can someone explain to me what exactly this question is asking? I am confused about this question as it says values less than x (=3) should be before 3, but then why 4 is appearing before 3 as it is >=3? Same doubt follows the second example as well as why 10 is before 5.

> 
> Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. If x is contained within the list, the values of x only need to be after the elements less than x.The partition element x can appear anywhere in the "right partition".
> 
> 
> For example,
> 
> 
> 

```
> Given 1->4->3->2->5->2 and x = 3, return 1->2->2->4->3->5.
> Given 3->5->8->5->10->2->1 and x = 5 return 3->1->2->10->5->5->8

```

Answer: 
The approach you followed is not transaction safe as well.

The best option I can think about is to use trigger

**Edit: According to @lagripe's mentionings**

```
CREATE TRIGGER sometrigger
AFTER INSERT ON sometable
BEGIN
  SET NEW.post_id := (SELECT id from sometable order by DESC limit 1) + 1 ; // you may need +1 here. I couldn't test it.
END

```

or you may consider to use `LAST_INSERT_ID`

```
insert into table_name values ( .... );
update table_name set post_id = LAST_INSERT_ID();

```

but why do you need two columns with the same id at first place?

if you really need why don't you use computed/generated columns?

```
CREATE TABLE Table1(
  id DOUBLE,
  post_id DOUBLE as (id)
);

```
you can use triggers :

```
CREATE TRIGGER UpdatePOST_id
BEFORE INSERT ON table_db
FOR EACH ROW 
SET NEW.post_id := (select id from table_db order by id DESC LIMIT 1)+1 ;

```

from now on, whatever you insert your as a value in post\_id column will be replaced with the id inserted automatically.

Test :

```
    |id|post_id|
    |20|  20   |
    |21|  21   |
    |22|  22   |
    |23|  23   |

```

To drop the trigger :

```
DROP trigger UpdatePOST_id

```