[LeetCode 题解]: 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的节点之前。

思路:

根据题意,很容易想到利用两个链表:

lower按照先后顺序存放小于x的节点;

higher存放大于等于x的节点。

最后合并分割后的链表,按照lower在前,higher在后的顺序。

class Solution {
public:
    ListNode *partition(ListNode *head, int x) {
        if(head==NULL || head->next==NULL) return head;
        // divide list into two parts:
        // lower part with all nodes whose value is less than x
        // higher part with all nodes whose value is more than x
        ListNode *lower = new ListNode(-1);
        ListNode *higher = new ListNode(-1);
        ListNode *ltail = lower;
        ListNode *htail = higher;
        ListNode *tmp = head;
        while(tmp){
            if(tmp->val < x){
                ltail->next = tmp;
                ltail = ltail->next;
            }else{
                htail->next = tmp;
                htail = htail->next;
            }
            tmp = tmp->next;
        }
        htail->next=NULL; // set end of a list
        ltail->next=higher->next; // append higher part to the tail of lower part
        return lower->next;
    }
};

[LeetCode 题解]: Partition List,布布扣,bubuko.com

时间: 2024-10-10 09:52:29

[LeetCode 题解]: Partition List的相关文章

(leetcode题解)Pascal&#39;s Triangle

Pascal's Triangle  Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 题意实现一个杨辉三角. 这道题只要注意了边界条件应该很好实现出来,C++实现如下 vector<vector<int>> generate(int

LeetCode --- 86. Partition List

题目链接: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,

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

LeetCode题解

Reverse Words in a String 考虑几个特殊的情况1.若字符窜s="  "2.字符窜s=“a  b  d     e”3.字符窜s=“ a” class Solution { public: void reverseWords(string &s) { int i; int cas=0; string st[100]; s+=' '; for(i=0;i<s.size();i++) { if(i==0 && s[0]==' ') con

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

[LeetCode] Palindrome Partition [11]

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&q

LeetCode——Palindrome Partition

Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a&q