题目链接:https://leetcode-cn.com/problems/partition-list/
题意:在保证原有顺序的情况下,把链表中比x小的排前面,比x大的排后面。
分析:链表排序https://www.cnblogs.com/qingjiuling/p/14017450.html的简化版
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode partition(ListNode head, int x) { ListNode small = new ListNode(0); ListNode smallHead = small; ListNode large = new ListNode(0); ListNode largeHead = large; while(head!=null){ if(head.val<x){ small.next=head; small=small.next; } else{ large.next=head; large=large.next; } head=head.next; } large.next=null; small.next=largeHead.next; return smallHead.next; } }