找LeetCode上简单的题目来撑过30天啦(DAY19)

**题号:86 标题:Partition List 难度:Medium

Given the head of 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.

Example 1:

Input: head = [1,4,3,2,5,2], x = 3
Output: [1,2,2,4,3,5]

Example 2:
Input: head = [2,1], x = 2
Output: [1,2]

Constraints:
• The number of nodes in the list is in the range [0, 200].
• -100 <= Node.val <= 100
• -200 <= x <= 200

我的程序码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode partition(ListNode head, int x) {
        if(head==null){
            return head;
        }
        ListNode temps = new ListNode(); //previous small
        ListNode tempeb = new ListNode(); //previous equal big
        ListNode small = new ListNode(); // str small 
        ListNode eqbi = new ListNode(); // str equal big
        ListNode Node_index = new ListNode(); //str
        ListNode ebst = new ListNode(-101); //head equal big
        ListNode sst = new ListNode(-101);//head small
        //root = head;

        Node_index = head;
        int chs=0,cheb=0;
        while(Node_index != null){
            if(Node_index.val<x){
                if(chs == 0 ){
                    small = Node_index;
                    sst =small;
                    temps = sst;
                    System.out.println("small: "+ Node_index.val);
                    Node_index = Node_index.next; 
                    chs++;
                }else{
                    temps.next = Node_index; 
                    small  = small.next;
                    temps = small;
                    System.out.println("small: "+ Node_index.val); 
                    Node_index = Node_index.next;
                }                 
            }else{
                if(cheb == 0 ){
                    eqbi = Node_index;
                    ebst = eqbi;
                    tempeb = ebst;
                    cheb++;
                    System.out.println("eqbi: "+ Node_index.val);
                    Node_index = Node_index.next;
                }else{
                    tempeb.next = Node_index; 
                    eqbi = eqbi.next;
                    tempeb = eqbi;
                    System.out.println("eqbi: "+ Node_index.val);
                    Node_index = Node_index.next;
                }
            }
        }
        eqbi.next = null;
        small.next = null;       
//         while(ebst != null){
//             System.out.println("ebst: "+ ebst.val);
//             ebst = ebst.next;
//         }
        
//         while(sst != null){
//             System.out.println("sst: "+ sst.val);
//             sst = sst.next;
//         }
        
            
        if(sst.val == -101){
            return ebst;
        }else if(ebst.val == -101){
            return sst;
        }else{
            temps.next = ebst;
            return sst;
        }
    }
}

DAY19心得
心态炸裂,不想打字


<<:  样式属性

>>:  【第19天】训练模型-验证与比较训练成果

【DAY 11】SharePoint 後记- 为什麽要选择 SharePoint?

哈罗大家好~ 关於 SharePoint 的应用,到昨天告一段落,回顾一下你可能会觉得,文件库、清单...

Day 5 追加测试

为了方便之後丢多点图进行测试,我将图片放进了 img 资料夹,并使用 glob 获得图片列表。 同时...

[Day29] 正规表达式 - 中英空白生成器实作

「曾经沧海难为水,中英不空眼睛痛」,每当看到文字没对齐、段落没缩排或者中文字遇英文字不加空白,就会感...

什麽是功能分解?

功能分解对应於各种功能关系,如原始复杂业务功能的开发方式。它主要关注如何开发整体功能及其各个组件之间...

[Day07]程序菜鸟自学C++资料结构演算法 – 链结串列实作应用

前言:讲解完链结串列的概念後,紧接着就要来进行实作了。 跟做阵列的时候一样,先创建一个新的专案,就可...