【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.

思路:

  设置两个链表头,遍历原链表,一个追加小数链表,一个追加大数链表,最后将小数链表粘到大数链表前边即为结果。

C++:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* partition(ListNode* head, int x) {
12         if(head == 0)
13             return 0;
14
15         ListNode* pless = 0;
16         ListNode* plesshead = 0;
17         ListNode* pgreat = 0;
18         ListNode* pgreathead = 0;
19         ListNode* phead = head;
20
21         while(phead != 0)
22         {
23             if(phead->val < x)
24             {
25                 if(pless == 0)
26                 {
27                     pless = phead;
28                     plesshead = pless;
29                 }
30                 else
31                 {
32                     pless->next = phead;
33                     pless = pless->next;
34                 }
35             }
36             else
37             {
38                 if(pgreat == 0)
39                 {
40                     pgreat = phead;
41                     pgreathead = pgreat;
42                 }
43                 else
44                 {
45                     pgreat->next = phead;
46                     pgreat = pgreat->next;
47                 }
48             }
49
50             phead = phead->next;
51         }
52
53         if(plesshead == 0)
54             return pgreathead;
55
56         if(pgreathead == 0)
57             return plesshead;
58
59         pless->next = pgreat->next = 0;
60         pless->next = pgreathead;
61
62         return plesshead;
63     }
64 };
时间: 2024-10-25 17:31:08

【LeetCode 86】Partition List的相关文章

【Leetcode 86】 Partition List

问题描述: 给定一个list, 将所有小于x的node放到左边,剩下的保持原样. 问题解决: 闲的无聊,用c++和python都做了一遍. 代码如下: # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def partition(self, head, x)

【LeetCode 229】Majority Element II

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space. 思路: [LeetCode 169]Majority Element 的拓展,这回要求的是出现次数超过三分之一次的数字咯,动动我们的大脑思考下,这样的数最多会存在几个呢,当然是2个嘛.因此,接着上一题的方

【LeetCode OJ】Sum Root to Leaf Numbers

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 # Definition for a  binary tree node # class TreeNode: #     def __init__(self, x): #         self.val = x #         self.left = No

【LeetCode OJ】Longest Consecutive Sequence

Problem Link: http://oj.leetcode.com/problems/longest-consecutive-sequence/ This problem is a classical problem where we can reduce the running time by the help of hash table. By given a list of numbers, we can find the longest consecutive sequence b

【LeetCode OJ】Word Ladder I

Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in this problem: Hash Table. One hash set is the words dictionary where we can check if a word is in the dictionary in O(1) time. The other hash set is us

【leetcode系列】String to Integer (atoi)

这个我就直接上代码了,最开始把"abc123"也算作合法的了,后来查了一下atoi的定义,把这种去掉了. public class Solution { public static int atoi(String inStr) { long result = 0L; /* * 网上查了一下,atoi函数的定义是如果第一个非空格字符存在,是数字或者正负号则开始做类型转换, * 之后检测到非数字(包括结束符\0)字符时停止转换,返回整型数.否则,返回零.可能的输入情况有: 1.空字符串 *

【leetcode系列】Valid Parentheses

很经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也可以直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class StackOne { LinkedList<Object> data; int top; int maxSize; StackOne(int size) { // TODO Auto-generated constructor stub top = -1; maxSize = 100; data = new

【leetcode系列】3Sum

这个题我最开始的思路是:先一个数定下来,然后在除这个数之外的集合里面找另外两个数,最后计算和.如此反复,对于N个数,需要进行N-2次循环. 我遇到的问题就是怎么找另外两个数,其实我想过参照Two Sum里面的解法,就是用Hashtable存,键值对的结构是<任意两个数的和,<下标1,下标2>>,但是构造这个Hashtable就需要O(N^2),后面真正解的时候有需要O(N^2). 参考了大牛的解法后,明白了找两个数还是用两个下标同时往中间移动比较好,下面上代码. import ja

【leetcode系列】Two Sum

解法一,我自己想的,有点弱,时间复杂度O(n^2). public class Solution { public int[] twoSum(int[] numbers, int target) { int[] result = new int[2]; for (int i = 0; i < numbers.length; i++) { for (int j = i + 1; j < numbers.length; j++) { if (numbers[i] + numbers[j] == t