leetcode 之Partition List(16)
思路就是定义两个链表,一个放大的,一个放小的,最后将两个连起来,注意细节问题。
ListNode *partionList(ListNode *head, int value) { ListNode left_dummy(-1); ListNode right_dummy(-1); auto left_cur = left_dummy.next; auto right_cur = right_dummy.next; for (ListNode *cur = head; cur != nullptr; cur = cur->next) { if (cur->val > value) { left_cur->next = cur; left_cur = cur; } else { right_cur->next = cur; right_cur = cur; } } left_cur->next = right_dummy.next; right_cur->next = nullptr; return left_dummy.next; }