链表分割
来源:互联网 发布:淘宝卖家签收拒绝退款 编辑:程序博客网 时间:2024/06/07 02:30
package 链表分割;import java.util.*;class ListNode { int val; ListNode next = null; ListNode(int val) { this.val = val; }}public class Partition { public ListNode partition(ListNode head, int x) { ListNode pHead = new ListNode(-1); ListNode pHead2 = new ListNode(-2); // first list ListNode firstHead = pHead; ListNode firstTail = pHead; // second list ListNode secondTail = pHead2; ListNode secondHead = pHead2; // ListNode traverse = head; while (traverse != null) { int val = traverse.val; if (val < x) { firstTail.next = traverse; firstTail = firstTail.next; traverse = traverse.next; firstTail.next = null; } else { if (val >= x) { secondTail.next = traverse; secondTail = secondTail.next; traverse = traverse.next; secondTail.next = null; } } } firstTail.next = secondHead.next; return firstHead.next; } public static void main(String[] args) { ListNode head = new ListNode(1); ListNode head1 = new ListNode(2); ListNode head2 = new ListNode(3); ListNode head3 = new ListNode(4); ListNode head4 = new ListNode(5); ListNode head5 = new ListNode(6); head.next = head5; head5.next = head1; head1.next = head4; head4.next = head2; head2.next = head3; print(new Partition().partition(head, 3)); } private static void print(ListNode head) { if(head == null){ return ; } else { System.out.print(head.val + " "); print(head.next); } }}
0 0
- 分割单向链表
- 分割链表
- 链表分割
- 链表分割
- 链表分割(Java)
- 链表分割
- 牛客网 | 链表分割
- 链表分割
- 链表分割
- 链表分割
- 链表分割问题
- 链表分割
- 链表分割
- 链表分割
- 链表分割
- 链表分割
- [牛客]链表分割
- 链表分割
- iOS Crash文件的解析
- PHP产生不重复随机数的5个方法总结
- sqlDependency监控数据库数据变化,自动通知
- Java 实现本地缓存工具
- 利用Album 实现仿微信图片选择(加入了图片剪裁)
- 链表分割
- JSch - Java实现的SFTP(文件下载详解篇)
- Websocket原理及与httpl跟 ajax轮询的区别
- 微信小程序开发-短信注册功能
- 第二章 CDN技术概述
- PHP高级——抽象类与接口的区别
- listview取消按下效果
- sublime3 BufferScroll插件实现折叠功能
- 百炼 2804 词典