左神算法笔记之链表——哈希表、有序表、单链表和双链表

这篇博客深入探讨了链表数据结构,包括哈希表的基础,有序表的概念,以及单链表和双链表的操作。重点讲述了如何反转单链表和双链表,实现时间复杂度为O(N)且额外空间复杂度为O(1)的方法。此外,还讨论了如何找到两个有序链表的公共部分,以及判断链表是否为回文结构的高效算法。同时,文章涵盖了按特定值划分链表、复制带有随机指针的链表以及解决两个链表相交问题的多种策略。
摘要由CSDN通过智能技术生成

一、哈希表

二、有序表

在这里插入图片描述

在这里插入图片描述

三、单链表和双链表

在这里插入图片描述

四、反转单链表和双链表(要求链表长度为N,时间复杂度为O(N),额外空间复杂度为O(1))

//反转单链表
public class ReverseLinkedList {
   
	//单链表结构
	public static class Node {
   
		public int value;
		public Node next;

		public Node(int value) {
   
			this.value = value;
		}
	}
	public static Node reverseLinkedList(Node head) {
   
		if(head == null){
   
			return null;
		}
		//一定要注意:
		//pre一直代表遍历到要反转节点的前驱节点
		//next一直代表遍历到要反转节点的后继节点
		Node pre = null; 
		Node next = null;
		while(head != null){
   
			//先用next保存head的下一个节点的信息
            //保证单链表不会因为反转head节点的原next节点而就此断裂
			next = head.next;
			//反转,head的后继变成向前指,指向head的next前驱
			head.next = pre;
			//节点的前驱和节点都后移,继续遍历
			pre = head;
			head = next;
		}
		//如果head为null的时候,pre就为最后一个节点了,就此链表已经反转完毕,pre就是反转后链表的第一个节点
		return pre;
	}
}
	
//反转双链表
public class ReverseDoubleLinkedList {
   
	public static class DoubleNode {
   
		int val;
		DoubleNode pre; //前驱
		DoubleNode next; //后继
		public DoubleNode (int value) {
   
			this.val = value;
		}
	}
	public static DoubleNode reverseDoubleLinkedList(DoubleNode head) {
   
		if(head == null){
   
			retrun null;
		}
		
		DoubleNode tmp = null;
		//res的作用仅仅是记录head的上一个节点
		DoubleNode res = null;
		
		while(head != null) {
   
			//先用tmp保持后继节点,然后交换前后节点
			tmp = head.next;
			head.next = head.pre;
			head.pre = tmp;
			//记录res
			res = head;
			//head指向tmp保存的原节点的后继节点,即向后推进一个节点
			head = tmp;
		}
		return res;
	}
}

五、给定两个有序链表的头指针head1和head2,打印两个链表的公共部分(要求:两个链表长度和为N,时间复杂度为O(N),空间复杂度为O(1))

public class PrintCommonPart {
   
    public static class Node {
   
        public int value;
        public Node next;

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

    public static void printCommonPart(Node head1, Node head2) {
   
        while (head1 != null && head2 != null) {
   
            if (head1.value < head2.value) {
   
                head1 = head1.next;
            } else if (head1.value > head2.value) {
   
                head2 = head2.next;
            } else {
   
                System.out.print(head1.value+" ");
                head1 = head1.next;
                head2 = head2.next;
            }
        }
        System.out.println();
    }
}

六、判断一个链表是否为回文结构

//判断是否是回文结构
public class IsPalindrome {
   
	//单链表结构
	public static class Node{
   
		public int value;
		public Node next;
		
		public Node(int value){
   
			this.value = date;
		}
	}
	//方法1:用栈结构存储整个链表元素,再依次弹出并与链表从头开始比较,空间复杂度:O(n)
	public static boolean isPalindrome1(Node head){
   
		if(head == null || head.next == null){
   
			return true;
		}
		//申请一个栈
		Stack<Node> stack = new Stack<Node>();
		Node cur = head;
		//将单链表元素按顺序入栈
		while(cur != null){
   
			stack.push(cur);
			cur = cur.next;
		}
		//不断弹出栈顶元素,并和单链表从头开始比较,不相等就返回false
		while(head != null){
   
			if(head.value != stack.pop().value){
   
				return false;
			}
			head = head.next
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值