开始刷leetcode day15:Partition List
来源:互联网 发布:linux卸载已安装软件 编辑:程序博客网 时间:2024/05/15 01:04
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
.
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) {
if(head == null) return null;
ListNode l1 = new ListNode(0);
ListNode l2 = new ListNode(0);
ListNode l3 = l1;
ListNode l4 = l2;
while(head != null)
{
if(head.val>= x)
{
l2.next = head;
head = head.next;
l2 = l2.next;
l2.next = null;
}else{
l1.next = head;
head = head.next;
l1 = l1.next;
l1.next = null;
}
}
l1.next = l4.next;
return l3.next;
}
}
- 开始刷leetcode day15:Partition List
- 开始刷leetcode day15:Valid Parentheses
- [leetcode刷题系列]Partition List
- LeetCode刷题笔录Partition List
- [LeetCode刷题记录]Partition List
- LeetCode: Partition List
- LeetCode Partition List
- LeetCode: Partition List
- [Leetcode] Partition List **
- [Leetcode] Partition List
- [LeetCode] Partition List
- leetcode 81: Partition List
- [Leetcode] Partition List
- leetcode 118: Partition List
- [LeetCode]Partition List
- [leetcode]Partition List
- LeetCode-Partition List
- [leetcode] Partition List
- 【坐在马桶上看算法】算法7:Dijkstra最短路算法
- linux命令(17)——gzip
- lspci源码(pciutils)实现分析
- C# 关键字 sealed
- 对Framework的初窥
- 开始刷leetcode day15:Partition List
- 第十一周阅读程序(2)
- 【坐在马桶上看算法】算法8:巧妙的邻接表(数组实现)
- 【Java编程】DOM XML Parser解析、遍历、创建XML
- 《How Tomcat Works》读书笔记(一)
- Android 开发 之 JNI入门 - NDK从入门到精通
- 就我不坑(nyoj1104)
- 【坐在马桶上看算法】算法9:开启“树”之旅
- Android JNI编程和NDK学习 --静态、动态两种方式实现JNI