【原创】leetCodeOj ---Partition List 解题报告

原题地址:

https://oj.leetcode.com/problems/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.

方法:

链表的基本功。

维护两个带头结点的链表,一个存 < ,一个存 >=,然后一个连接就可以了。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        if (head == null)
            return null;
        ListNode lessHead = new ListNode(0);
        ListNode moreHead = new ListNode(0);
        ListNode lessTail = lessHead;
        ListNode moreTail = moreHead;
        ListNode tmp = null;
        while (head != null)
        {
            tmp = head;
            head = head.next;
            tmp.next = null;
            if (tmp.val < x)
            {

                lessTail.next = tmp;
                lessTail = lessTail.next;
            }
            else
            {
                moreTail.next = tmp;
                moreTail = moreTail.next;
            }
        }
        if (lessHead.next != null)
            lessTail.next = moreHead.next;
        else
            lessHead.next = moreHead.next;
        return lessHead.next;
    }
}
时间: 2024-10-13 18:00:06

【原创】leetCodeOj ---Partition List 解题报告的相关文章

【原创】leetCodeOj --- Largest Number 解题报告

原题地址: https://oj.leetcode.com/problems/largest-number/ 题目内容: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may

【原创】leetCodeOj --- Majority Element 解题报告(脍炙人口的找n个元素数组中最少重复n/2次的元素)

题目地址: https://oj.leetcode.com/problems/majority-element/ 题目内容: Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. You may assume that the array is non-empty and the majority

【原创】leetCodeOj --- Dungeon Game 解题报告

原题地址: https://oj.leetcode.com/problems/dungeon-game/ 题目内容: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. The dungeon consists of M x N rooms laid out in a 2D grid. Our valiant knight (K) was init

【原创】leetCodeOj --- Sort List 解题报告

今日leetcode链表题全制霸 原题地址: https://oj.leetcode.com/problems/sort-list/ 题目内容: Sort List Sort a linked list in O(n log n) time using constant space complexity. 方法: 题目要求是链表排序,同时时间复杂度要求O(n log n),空间复杂度要求常数空间.这意味着你不可以把链表拷贝到数组中,用一个map保留值和指针的对应关系,最后在构造一个链表. 我们需

LeetCode: 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, Give

LeetCode: 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,Given

【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

【原创】ZOJ_1649 Rescue 解题报告

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want

【原创】leetCodeOj --- Sliding Window Maximum 解题报告

天,这题我已经没有底气高呼“水”了... 题目的地址: https://leetcode.com/problems/sliding-window-maximum/ 题目内容: Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the