(LeetCode 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的结点的前面。

要求不改变原链表结点的相对顺序。

解题思路:

在数组partition中,一般是通过首尾两个指针来进行前后遍历以及交换;

而在链表中,不需要进行元素的交换,可以通过创建两个新的头结点指针,来分别指向小于x的结点和大于等于x的结点,遍历结束之后,再将两个新的链表重新连接起来。

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        ListNode *left_head=NULL,*left_tail=NULL;
        ListNode *right_head=NULL,*right_tail=NULL;
        ListNode *p=head;

        while(p){
            if(p->val<x){
                if(left_tail){
                    left_tail->next=p;
                    left_tail=left_tail->next;
                }
                else
                    left_head=left_tail=p;
            }
            else{
                if(right_tail){
                    right_tail->next=p;
                    right_tail=right_tail->next;
                }
                else
                    right_head=right_tail=p;
            }
            p=p->next;
        }

        if(right_tail)
            right_tail->next=NULL;
        if(left_tail)
            left_tail->next=right_head;

        return left_head?left_head:right_head;
    }
};
时间: 2024-10-16 00:56:57

(LeetCode 86)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

ZigZag问题的一种新思路(Leetcode #6)

原始问题(leetcode ZigZag Conversion) 如果固定一个行数(这里是3),字符串 "PAYPALISHIRING" 用"之"字形的方式可以写成: P A H N A P L S I I G Y I R 这个结果按照一行一行访问的话是: "PAHNAPLSIIGYIR".给定一个字符串以及一个表示行数的整数,实现一个函数,返回按行序拼接而成的新串. string convert(string text, int nRows);

(LeetCode 78)SubSets

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. 题目要求 :求整数数组的所有子集 注意: 1.子集元素按非降序排列 2.不包含重复的子集 解题思路: 求解这类诸如子集的题目,都可以采用回溯法

(leetcode题解)Reshape the Matrix

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data. You're given a matrix represented by a two-dimensional array, and two positive integers r and c rep

(LeetCode 72)Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace

(leetcode题解)Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible. Example 1: Input: [1,4,3,2] Output: 4 Explan

leetcode || 86、Partition List

problem: 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

(leetcode题解)Can Place Flowers

Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0

(leetcode题解)Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has the largest sum = 6. 这是一道经典的题目,给定一个数组求和最大的子数组.算法导论对这道