程序员代码面试指南刷题--第二章.将单向链表按某值划分为左边小,中间相等,右边大的形式

题目描述

给定一个链表,再给定一个整数 pivot,请将链表调整为左部分都是值小于 pivot 的节点,中间部分都是值等于 pivot 的节点, 右边部分都是大于 pivot 的节点。
除此之外,对调整后的节点顺序没有更多要求。

输入描述:

第一行两个整数 n 和 pivot,n 表示链表的长度。

第二行 n 个整数 ai 表示链表的节点。

输出描述:

请在给定的函数内返回链表的头指针。

示例1
输入
5 3
9 0 4 5 1

输出
1 0 4 5 9

备注:

1≤n≤10000001000000≤ai​,pivot≤1000000

解法一:正常思路

import java.util.*;
import java.io.*;
public class Main{
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static ListNode creat(int len) throws IOException{
        String[] ss = br.readLine().trim().split(" ");
        ListNode head = new ListNode(0);
        ListNode r = head;
        for(int i=0;i<len;i++){
            ListNode node = new ListNode(Integer.parseInt(ss[i]));
            r.next = node;
            r = node;
        }
        return head.next;
    }
    public static ListNode change(ListNode head,int val){
        if(head==null||head.next==null) return head;
        ListNode lhead = new ListNode(0);
        ListNode midhead = new ListNode(0);
        ListNode rhead = new ListNode(0);
        ListNode l = lhead,mid = midhead,r = rhead; 
        ListNode tmp = null;
        while(head!=null){
            if(head.val<val){
                l.next = head;
                l = head;
            }else if(head.val==val){
                mid.next = head;
                mid = head;
            }else{
                r.next = head;
                r = r.next;
            }
            head = head.next;
        }
      
        r.next = null;
       if(lhead!=l) {
        	l.next = midhead.next;
        	mid = mid==midhead?l:mid;
        }
        if(mid!=midhead) {
        	mid.next = rhead.next;
        }
        return lhead!=l?lhead.next:(midhead!=mid?midhead.next:rhead.next);
    }
    public static void main(String[] args) throws IOException{
        String[] ss1 = br.readLine().trim().split(" ");
        ListNode head = creat(Integer.parseInt(ss1[0]));
        head = change(head,Integer.parseInt(ss1[1]));
        StringBuilder builder = new StringBuilder();
        while (head != null){
            builder.append(head.val).append(" ");
            head = head.next;
        }
        System.out.println(builder.toString());
       /*为啥会超时 透他妈(因为java输出太费时间)
        while(head!=null){
            System.out.print(head.val+" ");
            head = head.next;
        }
        */
    }
}
class ListNode{
	int val;
	ListNode next;
	public ListNode(int val) {
		this.val = val;
		this.next = null;
	}
}

解法二:类似红黄蓝

import java.util.*;
import java.io.*;
public class Main{
    public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    public static ListNode creat(int len) throws IOException{
        String[] ss = br.readLine().trim().split(" ");
        ListNode head = new ListNode(0);
        ListNode r = head;
        for(int i=0;i<len;i++){
            ListNode node = new ListNode(Integer.parseInt(ss[i]));
            r.next = node;
            r = node;
        }
        return head.next;
    }
    public static ListNode change(ListNode head,int val){
        if(head==null||head.next==null) return head;
        int count = 0;
        ListNode tmp = head;
        while(head!=null){
            head = head.next;
            count++;
        }
        ListNode[] array = new ListNode[count];
        for(int i=0;i<count;i++){
            array[i] = tmp;
            //System.out.println(array[i].val);
            tmp = tmp.next;
        }
        //类似荷兰国旗问题
        int l = -1;
        int r = count;
        int index = 0;
        while(index<r){
        	//System.out.println(array[index].val);
            if(array[index].val<val){
                swap(array,++l,index++);
            }else if(array[index].val>val){
                swap(array,--r,index);
            }else index++;
        }
        head = new ListNode(0);
        tmp = head;
        for(int i=0;i<count;i++){
            tmp.next = array[i];
            tmp = tmp.next;
        }
        tmp.next = null;
        return head.next;
    }
    public static void swap(ListNode[] array,int i,int j){
    	ListNode tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }
    public static void main(String[] args) throws IOException{
        String[] ss1 = br.readLine().trim().split(" ");
        ListNode head = creat(Integer.parseInt(ss1[0]));
        head = change(head,Integer.parseInt(ss1[1]));
        StringBuilder sb = new StringBuilder();
        while(head!=null){
            sb.append(head.val).append(" ");
            head = head.next;
        }
        System.out.print(sb.toString());
    }
}
class ListNode{
	int val;
	ListNode next;
	public ListNode(int val) {
		this.val = val;
		this.next = null;
	}
}

你可能感兴趣的:(程序员代码面试指南刷题)