将单链表划分成左边大右边小中间相等的形式 ---链表专题

链表专题

1. 将单链表划分成左边大右边小中间相等的形式

这里写图片描述

  1. 将节点遍历放入数组中,数组partion,然后再连接起来。
package zuochengyun;

public class ListPartition {
    public static class Node {
        private int data;
        private Node next;

        public Node(int data) {
            this.data = data;
        }

    }

    /**
     * 利用数组完成 空间复杂度0(n)
     * 
     * @param number
     * @param head
     * @return
     */
    public static Node listpartion1(int number, Node head) {
        int i = 0;
        Node cur = head;
        while (cur != null) {
            i++;
            cur = cur.next;
        }
        Node[] a = new Node[i];
        i = 0;
        cur = head;
        while (cur != null) {
            a[i] = cur;

                                // a[i].data=cur.data;
                i++;
            cur = cur.next;

        }



        arrpartion(a,  number);
        for (i = 1; i < a.length; i++) {
            a[i - 1].next = a[i];
        }
        a[i - 1].next = null;// 不赋值为零,有什么影响。。
        return a[0];
    }

    private static void arrpartion(Node[] a, int pivot) {
                int small = -1;
                int big = a.length-1;
                int i = 0;
                while (i != big) {
                    if (a[i].data < pivot) {
                        swap(a, ++small,i++);
                    } else if (a[i].data == pivot) {
                        i++;
                    } else {
                        swap(a,  big--,i);
                    }
                }
    }

    private static void swap(Node[] a, int i, int j) {
        Node temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

    public static void print(Node head) {
        while (head != null) {
            System.out.print("-" + head.data);
            head = head.next;
        }
        System.out.println();

    }

    public static void main(String[] args) {
        Node head1 = new Node(7);
        head1.next = new Node(9);
        head1.next.next = new Node(1);
        head1.next.next.next = new Node(8);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(2);
        head1.next.next.next.next.next.next = new Node(5);
        print(head1);
        Node head = listpartion1(5, head1);
        print(head);
    }

}

  1. 保证链表的稳定性
    public static Node listpartion2(Node head,int number) {
        if(head==null)
            return null;
        Node lessH=null;
        Node lessT=null;
        Node equalH=null;
        Node equalT=null;
        Node moreH=null;
        Node moreT=null;
        while(head!=null) {
            if(head.data<number) {
                if(lessH==null) {
                    lessH=head;
                    lessT=head;
                }else {
                    lessT.next=head;
                    lessT=lessT.next;

                }

            }else if(head.data==number){
                if(equalH==null) {
                    equalH=head;
                    equalT=head;
                }else {
                    equalT.next=head;
                    equalT=equalT.next;
                }

            }else {
                if(moreH==null) {
                    moreH=head;
                    moreT=head;
                }else {
                    moreT.next=head;
                    moreT=moreT.next;
                }

            }
            head=head.next;
        }
        /**
         * 不是很清楚
         */
        /**
         * 小段链接!
         */
        if(lessT!=null) {
            lessT=equalH;
            equalT= equalT==null?lessT:equalT;

        }
        //大段连接????
        if(equalT!=null) {
            equalT=moreH;

        }
        return null!=lessH?lessH:null!=equalH?equalH:moreH;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值