剑指offer计划链表

作者: 程序员khaos

剑指offer计划链表

从尾到头打印链表

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> num = new ArrayList();
        ArrayList<Integer> res = new ArrayList();
        while(listNode!=null){
            num.add(listNode.val);
            listNode=listNode.next;
        }
        for(int i = num.size()-1;i>=0; i --){
            res.add(num.get(i));
        }

        return res;
    }
}

反转链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        if(head==null) return null;
        ListNode pre = null;
        while(head!=null){
            ListNode next=head.next;
            head.next=pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}

合并两个排序的链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        if(list1==null) return list2;
        if(list2==null) return list1;
        if(list1.val<=list2.val) {
            list1.next=Merge(list1.next,list2);
            return list1;}
        else {
            list2.next=Merge(list1,list2.next);  
            return list2;}
    }
}

两个链表的第一个公共结点

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
import java.util.HashSet;

public class Solution {
    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
        HashSet<ListNode> hashset = new HashSet<ListNode>();
        if(pHead1==null || pHead2==null) return null;
        while(pHead1!=null) {
            hashset.add(pHead1);
             pHead1= pHead1.next;
        }
        while(pHead2!=null){
            if(hashset.contains(pHead2)) return pHead2;
            pHead2= pHead2.next;
        }
        return null;


    }
}

链表中环的入口结点

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
import java.util.*;
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead) {
        HashSet<ListNode> hashset = new HashSet();
        while(pHead!=null){
            if(hashset.contains(pHead)){
                return pHead;
            }
            hashset.add(pHead);
            pHead = pHead.next;
        }
        return null;

    }
}

链表中倒数最后k个结点

    public ListNode FindKthToTail(ListNode pHead, int k) {
        if (pHead == null || k == 0) {
            return null;
        }
        int count = 0;
        ListNode temp = pHead;
        while (temp!=null) {
            count ++ ;
            temp = temp.next;
        }
        if (count < k) {
            return null;
        }
        temp = pHead;
        for (int i = 0; i < count - k; i++) {
            temp = temp.next;
        }
        return temp;
    }

复杂链表的复制

/*
public class RandomListNode {
    int label;
    RandomListNode next = null;
    RandomListNode random = null;

    RandomListNode(int label) {
        this.label = label;
    }
}
*/
import java.util.*;
public class Solution {
        Map<RandomListNode, RandomListNode> cachedNode = new HashMap<RandomListNode, RandomListNode>();
        public RandomListNode Clone(RandomListNode pHead) {
         if (pHead == null)   return null;
        if (!cachedNode.containsKey(pHead)) {
            RandomListNode headNew = new RandomListNode(pHead.label);
            cachedNode.put(pHead, headNew);
            headNew.next = Clone(pHead.next);
            headNew.random = Clone(pHead.random);
        }
        return cachedNode.get(pHead); 
    }
}

删除链表中重复的结点

public class Solution {
    public ListNode deleteDuplication(ListNode pHead) {
        if (pHead == null || pHead.next == null) { // 只有0个或1个结点,则返回
            return pHead;
        }
        if (pHead.val == pHead.next.val) { // 当前结点是重复结点
            ListNode pNode = pHead.next;
            while (pNode != null && pNode.val == pHead.val) {
                // 跳过值与当前结点相同的全部结点,找到第一个与当前结点不同的结点
                pNode = pNode.next;
            }
            return deleteDuplication(pNode); // 从第一个与当前结点不同的结点开始递归
            pHead.next = deleteDuplication(pHead.next); // 保留当前结点,从下一个结点开始递归
            return pHead;
        }
    }
}

原文创作:程序员khaos

原文链接:https://www.cnblogs.com/urmkhaos/p/15394290.html

更多推荐

更多
  • Go编程秘籍-三、数据转换与组合 本章将展示一些在数据类型之间转换、使用非常大的数字、使用货币、使用不同类型的编码和解码(包括 Base64 和gob)以及使用闭包创建自定义集合的示例。转换数据类型和接口转换,使用 math 和 math/big ...
  • Go编程秘籍-一、I/O 和文件系统 Go 为基本和复杂 I/O 提供了极好的支持。本章中的方法将探索用于处理 I/O 的常见 Go 接口,并向您展示如何使用它们。Go 标准库经常使用这些接口,本书中的食谱都将使用这些接口。本章将介绍以下配方:使用公共 I/O ...
  • Go编程秘籍-二、命令行工具 命令行应用是处理用户输入和输出的最简单方法之一。本章将重点介绍基于命令行的交互,如命令行参数、配置和环境变量。最后,我们将介绍一个在 Unix 和 Bash for Windows 中为文本输出着色的库。
  • Go编程秘籍-零、前言 要使用本书,您需要以下内容:Unix 编程环境。Go 1.x 系列的最新版本。互联网连接。如各章所述,允许安装其他软件包。各章节的技术要求部分中提到了各配方的先决条件和其他安装要求。
  • Go编程秘籍-十一、分布式系统 本章将探讨管理分布式数据、编排、容器化、度量和监视的方法。这些将成为编写和维护微服务和大型分布式应用工具箱的一部分。在本章中,我们将介绍以下配方:与 concur 一起使用服务发现,利用 Raft 实现基本共识,与 Docker ...
  • Go编程秘籍-十、并行与并发 Go 提供了使并行应用成为可能的原语。Goroutines 允许任何函数变成异步和并发的。通道允许应用设置与 Goroutines 的通信。在本章中,我们将介绍以下配方:使用通道和 select 语句,使用 sync.WaitGroup ...
  • Go编程秘籍-十二、反应式编程和数据流 本章还将探讨与卡夫卡联系的各种方式,并使用它来处理信息。最后,本章将演示如何在 Go 中创建一个基本的graphql服务器。在本章中,我们将介绍以下配方:使用 Goflow 进行数据流编程,与 Kafka 一起使用异步生产者,将卡夫卡连接到...
  • Go编程秘籍-九、测试 Go 代码 本章将分享一些测试 Go 代码的方法。在本章中,我们将介绍以下配方:使用标准库进行模拟,使用 Mockgen 包模拟接口,使用表驱动测试提高覆盖率,使用第三方测试工具,使用 Go 进行行为测试,在 Go ...
  • Go编程秘籍-十三、无服务器编程 本章将重点介绍无服务器架构,并将其与 Go 语言结合使用。无服务器架构是开发人员不管理后端服务器的架构。这包括亚马逊 Lambda、谷歌应用引擎和 Firebase 等服务。这些服务允许您在 web 上快速部署应用和存储数据。Apex ...
  • Go编程秘籍-五、网络编程 Go 标准库为网络操作提供了大量支持。它包括允许您使用 HTTP 管理 TCP/IP、UDP、DNS、邮件和 RPC 的包。包括gorilla/websockets,用于可在正常 HTTP 处理程序中使用的 WebSocket ...
  • 近期文章

    更多
    文章目录

      推荐作者

      更多