ARTS-3-算法练习-基于链表的插入排序和链表重排

18 篇文章 0 订阅

概述:

左耳朵耗子专栏《左耳听风》 用户自发每周完成一个ARTS:

1.Algorithm:每周至少做一个 leetcode 的算法题

2.Review:阅读并点评至少一篇英文技术文章

3.Tip:学习至少一个技术技巧

4.Share:分享一篇有观点和思考的技术文章

 

Algorithm
题目概述:

Sort a linked list using insertion sort.

 

 

思路分析:

基于链表来实现插入排序,这里创建了一个新的链表空间用于存储元素

package 算法.链表;

/**
 * 基于链表进行的插入排序
 *
 * @author idea
 * @data 2019/4/15
 *
 */
public class LinkedListSort {

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }

        ListNode(int x,ListNode next) {
            val = x;
            this.next = next;
        }
    }

    public void insertData(int val, ListNode head) {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = new ListNode(val);
    }

    public void display(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }

    /**
     * 插排
     *
     * @param head
     * @return
     */
    public ListNode insertionSortList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode newNodeList = new ListNode(Integer.MAX_VALUE);
        ListNode tempNode = head;

        while (tempNode != null) {
            ListNode cur = newNodeList;
            while (cur.next != null && cur.next.val < tempNode.val) {
                cur = cur.next;
            }
            ListNode next = cur.next;
            cur.next=new ListNode(tempNode.val,next);
            tempNode=tempNode.next;
        }
        return newNodeList.next;
    }

    public static void main(String[] args) {
        LinkedListSort linkedListSort = new LinkedListSort();
        ListNode l = new ListNode(1);
        linkedListSort.insertData(412, l);
        linkedListSort.insertData(22, l);
        linkedListSort.insertData(155, l);
        linkedListSort.insertData(22, l);
        linkedListSort.insertData(41, l);
        linkedListSort.display(l);
        System.out.println("-------------");
        ListNode newList = linkedListSort.insertionSortList(l);
        linkedListSort.display(newList);

    }
}

第二题:

Given a singly linked list LL 0→L 1→…→L n-1→L n,
reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…

You must do this in-place without altering the nodes' values.

For example,
Given{1,2,3,4}, reorder it to{1,4,2,3}.

代码

package 算法.链表;

/**
 * @author idea
 * @data 2019/4/17
 * @des Given a singly linked list L: L 0→L 1→…→L n-1→L n,
 * reorder it to: L 0→L n →L 1→L n-1→L 2→L n-2→…
 * <p>
 * You must do this in-place without altering the nodes' values.
 * <p>
 * For example,
 * Given{1,2,3,4}, reorder it to{1,4,2,3}.
 */


public class LinkedListReOrder {


    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    public void insertData(int val, ListNode head) {
        ListNode cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = new ListNode(val);
    }

    public void display(ListNode head) {
        ListNode cur = head;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }


    public void reorderList(ListNode head) {
        if(head == null || head.next == null) {
            return;
        }
        // 快满指针找到中间节点
        ListNode fast = head;
        ListNode slow = head;
        while(fast.next != null && fast.next.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        // 拆分链表,并反转中间节点之后的链表
        ListNode after = slow.next;
        slow.next = null;
        ListNode pre = null;
        while(after != null){
            ListNode temp = after.next;
            after.next = pre;
            pre = after;
            after = temp;
        }
        // 合并两个链表
        ListNode first = head;
        after = pre;
        while(first != null && after != null){
            ListNode ftemp = first.next;
            ListNode aftemp = after.next;
            first.next = after;
            first = ftemp;
            after.next = first;
            after = aftemp;
        }
    }

    public static void main(String[] args) {
        LinkedListReOrder reOrder = new LinkedListReOrder();
        ListNode l = new ListNode(1);
        reOrder.insertData(412, l);
        reOrder.insertData(22, l);
        reOrder.insertData(155, l);
        reOrder.insertData(22, l);
        reOrder.insertData(41, l);
        reOrder.display(l);
        System.out.println("-------------");
        reOrder.reorderList(l);
        reOrder.display(l);

    }


}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值