86. Partition List
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.
For example,
Given 1->4->3->2->5->2 and x = 3,
return 1->2->2->4->3->5.
题意:
给定一个链表和一个值x,通过x划分链表中所有节点,使链表前面的节点小于X,后面的节点大于或等于x,要保持原始的相对在一个分区的节点顺序。
思路:
首先创建两个单链表的头结点,分别用来指向小于x的单链表,和大于等于x的单链表,最后把后者连接到前者的单链表上,这样原单链表的相对顺序也不会改变。 注意–第二个链表的末尾要置为NULL。
1 | ListNode* partition(ListNode* head, int x) { |