题目
思路
- 使用虚拟节点和尾插法
代码
分割链表public ListNode partition(ListNode head, int x) {ListNode p1 = new ListNode(0);ListNode p2 = new ListNode(0);ListNode p3 = p1, p4 = p2;while (head != null) {if (head.val < x) {p3.next = head;p3 = p3.next;} else {p4.next = head;p4 = p4.next;}head = head.next;}p3.next = p2.next;p4.next = null;return p1.next;}
