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并且该节点的next节点的值大于等于x,这样的两个节点分别记为S和L,然后从L开始遍历链表,将小于x的值插入到S和L之间,并且更新S,L并不改变。这个想法值得注意的几点,第一个,head节点的值就大于x,则S为NULL,所有比x小的节点都要插入到Head之前,当插入第一个之后需要更新头节点,此后S不为NULL;第二个值得注意的就是每次插入SL之间后,需要更新S,否则出错。AC代码如下:

ListNode* partition(ListNode* head, int x) {
        if(!head)
            return head;
        //find two nodes
        ListNode * s=head;
        ListNode * l=NULL;
        if(head->val >= x)
        {
            l = head;
            s = NULL;
        }
        else
        {
            while(s->next!=NULL && s->next->val <x )
                s=s->next;
            if(s->next == NULL)
                return head;
            l=s->next;
        }
        //search node less than x from node L
        ListNode *fir=l;
        ListNode *sec=fir->next;
        for(;sec!=NULL;)
        {
            if(fir->val>=x && sec->val < x)
            {
                fir->next = sec->next;
                if(s)
                {
                    s->next = sec;
                    sec->next = l;
                    s= sec;
                }
                else
                {
                    sec->next = l;
                    head = sec;
                    s= sec;
                }
                sec = fir->next;
            }
            else
            {
                fir = fir->next;
                sec = sec->next;
            }
        }
        return head;
    }
时间: 2024-09-29 10:42:54

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

LeetCode【9】. Palindrome Number --java的实现

Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra spa

LeetCode【5】. Longest Palindromic Substring --java实现

Longest Palindromic Substring 一.题目如下:        Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 题目要求给定字符串的最大对称子字符串,如"aaabccbac

LeetCode【2】. Add Two Numbers--java实现

第二道题 Add Two Numbers 如下:         You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.     Inpu

Sort - leetcode 【排序】

215. Kth Largest Element in an Array 4 C++ Solutions using Partition, Max-Heap, priority_queue and multiset respectively Well, this problem has a naive solution, which is to sort the array in descending order and return the k-1-th element. class Solu

【链表】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-&

LeetCode【3】.Longest Substring Without Repeating Characters--算法图解及java实现

第三道题Longest Substring Without Repeating Characters如下:     Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", whic

LeetCode【4】. Median of Two Sorted Arrays --java的不同方法实现

Median of Two Sorted Arrays 这道题确实有点难,想挺久,看别人答案也是不能一下子就明白.题目难度为Hard,原题如下: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). 给定两个已