Partition List; Linked List; Pointer;

At the first sight of the problem, I misunderstood it as sort the former part of the list and then keep the original order of those nodes in the larger or equal to target value part. Thus, I sort the first part and get a wrong result.

Code:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode cur = head;
        ListNode head1 = null, tail1 = null;
        ListNode head2 = null, tail2 = null;
        while(cur != null){
            ListNode nex = cur.next;
            ListNode node = cur;
            if(node.val < x){
                ListNode cur1 = head1;
                ListNode prev1 = null;
                while(cur1 != null && cur1.val < node.val) {
                    prev1 = cur1;
                    cur1 = cur1.next;
                }
                if(prev1 != null) prev1.next = node;
                else head1 = node;
                cur.next = cur1;
                if(cur1 == null) tail1 = node;
            }
            else{
                ListNode cur2 = tail2;
                if(cur2 != null){
                    tail2.next = node;
                    node.next = null;
                    tail2 = node;
                }
                else{
                    head2 = node;
                    tail2 = node;
                    node.next = null;
                }
            }
            cur = nex;
        }
        if(tail1 != null) tail1.next = head2;
        if(head1 == null) return head2;
        return head1;
    }
}

After a littile modification, we can the the right one:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode cur = head;
        ListNode head1 = null, tail1 = null;
        ListNode head2 = null, tail2 = null;
        while(cur != null){
            ListNode nex = cur.next;
            ListNode node = cur;
            if(node.val < x){
                ListNode cur1 = tail1;
                if(cur1 != null){
                    tail1.next = node;
                    node.next = null;
                    tail1 = node;
                }
                else{
                    head1 = node;
                    tail1 = node;
                    node.next = null;
                }
            }
            else{
                ListNode cur2 = tail2;
                if(cur2 != null){
                    tail2.next = node;
                    node.next = null;
                    tail2 = node;
                }
                else{
                    head2 = node;
                    tail2 = node;
                    node.next = null;
                }
            }
            cur = nex;
        }
        if(tail1 != null) tail1.next = head2;
        if(head1 == null) return head2;
        return head1;
    }
}
时间: 2024-08-06 16:03:54

Partition List; Linked List; Pointer;的相关文章

Jan 09 - Remove Linked List Elements; Linked List; Pointer operation;

数据结构:单向链表.指针操作,注意Null Pointer的情况 以及链表头指针的操作 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode removeElements(ListNode head,

Swap Nodes in Pairs; Data Structure; Linked List; Pointer;

注意用指针记录节点的前一个节点位置. 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode swapPairs(ListNode head) { if(head == null || head.nex

Jan 12 - Delete Node in a Linked List; Data Structure; Linked List; Pointer;

代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void deleteNode(ListNode node) { if(node == null) return; while(node.next != null)

Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration &amp; Recursion

Iteration: 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseList(ListNode head) { if(head == null) return null; List

uCos-II内存管理

ucos 系统由于构思巧妙,结构精简设计,可读性强,同时又具有实时性操作系统大部分的优点,在 物联网开发中应用非常广泛. 之前一直都只是会用ucos 却没有好好研究过它,最近项目中要用到了 ucos-II 所以顺便研究了一番,突然发现 ucos-II 的内存管理写得非常巧妙. 废话不多说,直接上代码: 先看一个内存块结构体 1 typedef struct os_mem {                   /* MEMORY CONTROL BLOCK                   

uC/OS-II内存(OS_mem)块

/***********************************************************************************************************                                                uC/OS-II*                                          The Real-Time Kernel*                      

c的指针(浅见)

1.变量:一段存储区域,每个变量都有一个地址,指向变量的存储位置. 变量名:变量名对应变量的地址.(编译器会将程序中的变量地址和变量名,建立一个"符号表",编写代码时对变量的操作,实际编译器会拿 符号表中变量名对应的 地址中存储的数据进行操作.) 2.指针是个变量,变量里面存储的值用来当作地址. char *p; p是变量,存储的值是一个地址.(p本身也有地址,&p) 3.二维指针: char **p; p存储的是一个指针的地址.(p本身也有地址,&p) 4.指针和数组

CTCI 2.4

Write code to partition a linked list around a value x, such that all nodes less than x come before all nodes greater than or equal to x. Use two additional pointer to store the nodes less than x and the nodes equal to or greater than x. Then link th

List------Linked 链表

1.Definition Linked list consists of a series of nodes. Each nodes contains the element and a pointer which points to the next node. The last node's next link points to NULL. Linked=data+pointer use the pointer to describe the logical relationship 2.