博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
86. Partition List
阅读量:4974 次
发布时间:2019-06-12

本文共 3852 字,大约阅读时间需要 12 分钟。

题目:

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小的放入headA,其余放入headB,最后把两个链表接起来。 要注意把右侧链表的下一节点设置为null。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {    public ListNode partition(ListNode head, int x) {        if(head == null || head.next == null)            return head;        ListNode headLeft = new ListNode(-1);        ListNode headRight = new ListNode(-1);        ListNode nodeLeft = headLeft;        ListNode nodeRight = headRight;                while(head != null){            if(head.val < x){                nodeLeft.next = head;                nodeLeft = nodeLeft.next;            } else {                nodeRight.next = head;                nodeRight = nodeRight.next;            }                            head = head.next;        }                nodeRight.next = null;        nodeLeft.next = headRight.next;        return headLeft.next;    }}

 

Update: 昨晚前面两道难题,终于有简单题换换口味了...好高兴 -_____-!!

/** * 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) {        if(head == null || head.next == null)            return head;        ListNode dummyLeft = new ListNode(-1);        ListNode dummyRight = new ListNode(-1);        ListNode left = dummyLeft;        ListNode right = dummyRight;                while(head != null) {            if(head.val < x) {                left.next = head;                left = left.next;            } else {                right.next = head;                right = right.next;            }            head = head.next;        }                right.next = null;        left.next = dummyRight.next;                return dummyLeft.next;    }}

 

二刷:

这道题比较简单,创建两个fakeHead,以及两个runner,直接one pass过就可以了

Java:

Time Complexity - O(n), Space Complexity - O(1)

/** * 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) {        if (head == null || head.next == null) {            return head;        }        ListNode leftHead = new ListNode(-1), rightHead = new ListNode (-1);        ListNode left = leftHead, right = rightHead;        while (head != null) {            if (head.val < x) {                left.next = head;                left = left.next;            } else {                right.next = head;                right = right.next;            }            head = head.next;        }        right.next = null;        left.next = rightHead.next;        return leftHead.next;    }}

 

 

三刷:

创建两个表头,三个runner,然后遍历时node的值跟x进行比较,最后再merge就可以了。

Java:

/** * 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 leftHead = new ListNode(-1);        ListNode rightHead = new ListNode(-1);        ListNode node = head, left = leftHead, right = rightHead;        while (node != null) {            if (node.val < x) {                left.next = node;                left = left.next;            } else {                right.next = node;                right = right.next;            }            node = node.next;        }        right.next = null;        left.next = rightHead.next;        return leftHead.next;    }}

 

 

测试:

Reference:  http://www.cnblogs.com/springfor/p/3862392.html

转载于:https://www.cnblogs.com/yrbbest/p/4437143.html

你可能感兴趣的文章
Sublime 快捷键
查看>>
GNU make manual 翻译(二十六)
查看>>
poj1436
查看>>
iOS 电话在后台运行时,我的启动图片被压缩
查看>>
MySQL修复打不开的视图定义
查看>>
PHP max_execution_time 超时
查看>>
NTBootAutofix:一款极为优秀的自动修复XP/VISTA/WIN7系统引导的工具
查看>>
js获取对象、数组的实际长度,元素实际个数
查看>>
asp.net 网站监控方案
查看>>
jquery 日期选择的方案
查看>>
Java数据类型和方法参数
查看>>
初学者可能不知道的vue技巧
查看>>
USACO 4.1.1 麦香牛块 Beef McNuggets
查看>>
linux每日命令(39):lsof命令
查看>>
HRESULT:0x80070057 (E_INVALIDARG) 异常
查看>>
查询数据库中指定数据库所有表中是否包含指定字段
查看>>
Cool Websites
查看>>
怎样取消不能改动(仅仅读打开)的word文件的password
查看>>
58 子类继承父类的方法重写
查看>>
61 package
查看>>