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

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

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 L: L 0→L 1→…→Ln-1→L n,

reorder it to: L 0→LnL 1→Ln-1→L 2→Ln-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);
    }
}


目录
相关文章
|
7月前
|
存储 算法 物联网
解析局域网内控制电脑机制:基于 Go 语言链表算法的隐秘通信技术探究
数字化办公与物联网蓬勃发展的时代背景下,局域网内计算机控制已成为提升工作效率、达成设备协同管理的重要途径。无论是企业远程办公时的设备统一调度,还是智能家居系统中多设备间的联动控制,高效的数据传输与管理机制均构成实现局域网内计算机控制功能的核心要素。本文将深入探究 Go 语言中的链表数据结构,剖析其在局域网内计算机控制过程中,如何达成数据的有序存储与高效传输,并通过完整的 Go 语言代码示例展示其应用流程。
141 0
|
8月前
|
存储 监控 算法
员工电脑监控系统中的 C# 链表算法剖析-如何监控员工的电脑
当代企业管理体系中,员工电脑监控已成为一个具有重要研究价值与实践意义的关键议题。随着数字化办公模式的广泛普及,企业亟需确保员工对公司资源的合理利用,维护网络安全环境,并提升整体工作效率。有效的电脑监控手段对于企业实现这些目标具有不可忽视的作用,而这一过程离不开精妙的数据结构与算法作为技术支撑。本文旨在深入探究链表(Linked List)这一经典数据结构在员工电脑监控场景中的具体应用,并通过 C# 编程语言给出详尽的代码实现与解析。
168 5
|
9月前
|
存储 机器学习/深度学习 算法
C 408—《数据结构》算法题基础篇—链表(下)
408考研——《数据结构》算法题基础篇之链表(下)。
343 30
|
9月前
|
存储 算法 C语言
C 408—《数据结构》算法题基础篇—链表(上)
408考研——《数据结构》算法题基础篇之链表(上)。
445 25
|
9月前
|
存储 监控 算法
公司监控上网软件架构:基于 C++ 链表算法的数据关联机制探讨
在数字化办公时代,公司监控上网软件成为企业管理网络资源和保障信息安全的关键工具。本文深入剖析C++中的链表数据结构及其在该软件中的应用。链表通过节点存储网络访问记录,具备高效插入、删除操作及节省内存的优势,助力企业实时追踪员工上网行为,提升运营效率并降低安全风险。示例代码展示了如何用C++实现链表记录上网行为,并模拟发送至服务器。链表为公司监控上网软件提供了灵活高效的数据管理方式,但实际开发还需考虑安全性、隐私保护等多方面因素。
190 0
公司监控上网软件架构:基于 C++ 链表算法的数据关联机制探讨
|
算法 安全 搜索推荐
2024重生之回溯数据结构与算法系列学习之单双链表精题详解(9)【无论是王道考研人还是IKUN都能包会的;不然别给我家鸽鸽丢脸好嘛?】
数据结构王道第2.3章之IKUN和I原达人之数据结构与算法系列学习x单双链表精题详解、数据结构、C++、排序算法、java、动态规划你个小黑子;这都学不会;能不能不要给我家鸽鸽丢脸啊~除了会黑我家鸽鸽还会干嘛?!!!
|
存储 Web App开发 算法
2024重生之回溯数据结构与算法系列学习之单双链表【无论是王道考研人还是IKUN都能包会的;不然别给我家鸽鸽丢脸好嘛?】
数据结构之单双链表按位、值查找;[前后]插入;删除指定节点;求表长、静态链表等代码及具体思路详解步骤;举例说明、注意点及常见报错问题所对应的解决方法
|
存储 缓存 算法
经典算法之链表篇(三)
经典算法之链表篇(三)
204 4
|
算法
经典算法之链表篇(二)
经典算法之链表篇(二)
222 4
|
算法 索引
经典算法之链表篇
经典算法之链表篇
184 4

热门文章

最新文章

下一篇
oss云网关配置