给定一个链表,再给定一个整数 pivot,请将链表调整为左部分都是值小于 pivot 的节点,中间部分都是值等于 pivot 的节点, 右边部分都是大于 pivot 的节点。
除此之外,对调整后的节点顺序没有更多要求。
第一行两个整数 n 和 pivot,n 表示链表的长度。
第二行 n 个整数 ai 表示链表的节点。
请在给定的函数内返回链表的头指针。
5 3 9 0 4 5 1
1 0 4 5 9
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int pivot = in.nextInt(); Node head = new Node(in.nextInt()); Node cur = head; for (int i = 1; i < n; i++) { cur.next = new Node(in.nextInt()); cur = cur.next; } head = listPartition(head, pivot); while (head != null) { System.out.print(head.val + " "); head = head.next; } } public static Node listPartition(Node head, int pivot) { Node sH = null; Node sT = null; Node eH = null; Node eT = null; Node mH = null; Node mT = null; Node next = null; while (head != null) { next = head.next; head.next = null; if (head.val < pivot) { if (sH == null) { sH = head; sT = head; } else { sT.next = head; sT = head; } } else if (head.val == pivot) { if (eH == null) { eH = head; eT = head; } else { eT.next = head; eT = head; } } else { if (mT == null) { mH = head; mT = head; } else { mT.next = head; mT = head; } } head = next; } if (sT != null) { sT.next = eH; eT = eT == null ? sT : eT; } if (eT != null) { eT.next = mH; } return sH != null ? sH : (eH != null ? eH : mH); } } class Node { int val; Node next; Node(int val) { this.val = val; this.next = null; } }
import java.util.Scanner; /** * * @author minghai * @date 2019年9月6日 */ public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int pivot = scan.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scan.nextInt(); } arrPartition(arr,pivot); for(int a:arr){ System.out.print(a+" "); } } private static void arrPartition(int[] nodeArr, int pivot) { int small = -1; int big = nodeArr.length; int index = 0; while(index != big) { if(nodeArr[index] < pivot) { swap(nodeArr,++small,index++); }else if(nodeArr[index] == pivot) { index++; }else { swap(nodeArr, --big, index); } } } private static void swap(int[] nodeArr, int i, int j) { int temp = nodeArr[i]; nodeArr[i] = nodeArr[j]; nodeArr[j] = temp; } }方法2:还是超时,不过思路很好,可以实现数据的稳定性,代码为 左神《程序员代码面试指南》中此题金阶题的代码,不过在此OJ测试中,超时,可以通过在从控制台输入时,每取到一个值就判断该值与目标值的大小或相等,然后加到对应的 大、中、小链表中,可以减少一次时间一次循环时间。
package ch02_linked; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int pivot = scan.nextInt(); Node head = new Node(scan.nextInt()); Node cur = head; while(--n > 0) { cur.next = new Node(scan.nextInt()); cur = cur.next; } cur = listPartition2(head, pivot); while(cur!=null) { System.out.print(cur.val+" "); cur = cur.next; } } public static Node listPartition2(Node head, int pivot) { Node sH = null; Node sT = null; Node eH = null; Node eT = null; Node bH = null; Node bT = null; Node next = null; while(head != null) { next = head.next; head.next = null; if(head.val < pivot) { if(sH == null) { sH = head; sT = head; }else { sT.next = head; sT = sT.next; } }else if(head.val == pivot) { if(eH == null) { eH = head; eT = head; }else { eT.next = head; eT = eT.next; } }else { if(bH == null) { bH = head; bT = head; }else { bT.next = head; bT = bT.next; } } head = next; } if(sT != null) { sT.next = eH; eT = eT == null ? sT : eT; } if(eT != null) { eT.next = bH; } return sH != null ? sH : eH != null ? eH : bH; } }