链表

链表

1.链表的结构

public class Node{
	int value;
	Node next;
	public Node(int data){
		value=data;
	}
}

2.链表的单向反转

public class Reverse_One{
	public static void printLinkedList(Node head){
		while(head!=null){
			System.out.println(head.value+" ");
			head=head.next;
		}
	}
	public static Node reverseLinkedList(Node head){
		Node pre=null;
		Node next=null;
		while(head!=null){
			next=head.next;
			head.next=pre;
			pre=head;
			head=next;
		}
		return pre;
	}
}

3.链表的双向反转

public class Node{
	int value;
	Node next;
	Node last;
	public Node(int data){
		value=data;
	}
}
public static Node reverseLinkedList(Node head){
	Node pre=null;
	Node next=null;
	while(head!=null){
		next=head.next;
		head.next=pre;
		head.last=next;
		pre=head;
		head=next;
	}
	return pre;
}

4.打印公共部分的链表

public class PrintCommonPart{
	public static void printcommonpart(Node head1,Node head2){
		while(!(head1==null||head2==null)){
			return;
		}
		if(head1.value==head2.value){
			head1=head1.next;
			head2=head2.next;
		}
		else if(head1.value>head2.value){
			head2=head2.next;
		}
		else{
			head1=head1.next;
		}
	}
}

5.判断是否为回文链表(两种方法)

//方法1
public class Palindrome_Linked_List{
	public static boolean isHuiWen(Node head){
		Stack<Node> sta=new Stack<>();
		Node temp=head;
		while(temp!=null){
			sta.push(temp);
			temp=temp.next;
		}
		if(head!=null){
			if(head.value!=sta.pop().value){
				return false;
			}
			head=head.next;
		}
		return true;
	}
}
//方法2
public static boolean isHuiWen(Node head){
	Node fast=null;
	Node low=null;
	boolean result=true;
	while(fast.next!=null&&fast.next.next!=null){
		low=low.next;
		fast=fast.next.next;
	}
	Node rightFirst=low.next;
	Node next=null;
	low.next=null;
	while(rightFirst!=null){
		next=rightFirst.next;
		rightFirst.next=low;
		low=rightFirst;
		rightFirst=next;
	}
	Node useHead=head;
	Node uselow=low;
	while(!(useHead==null||uselow==null)){
		if(useHead.value!=uselow.value){
			result=false;
			break;
		}
		useHead=useHead.next;
		uselow=uselow.next;
	}
	while(low!=null){
		next=low.next;
		low.next=rightFirst;
		rightFirst=low;
		low=next;
	}
	return result;
}

6.三项切分链表

public static Node listPartition(Node head,int num){
	Node smallStart=null;
	Node smallEnd=null;
	Node equalStart=null;
	Node equalEnd=null;
	Node bigStart=null;
	Node bigEnd=null;
	while(head!=null){
		if(head.value<num){
			if(smallStart==null){
				smallStart=head;
				smallEnd=head;
			}
			smallEnd.next=head;
			smallEnd=head;
		}
		else if(head.value==num){
			if(equalStart==null){
				equalStart=head;
				equalEnd=head;
			}
			equalEnd.next=head;
			equalEnd=head;
		}
		else{
			if(bigStart==null){
				bigStart=head;
				bigEnd=head;
			}
			bigEnd.next=head;
			bigEnd=head;
		}
		head=head.next;
	}
	if(smallEnd!=null){
		smallEnd.next=equalStart;
		equalEnd=equalEnd==null?smallEnd:equalEnd;
	}
	if(equalEnd!=null){
		equalEnd.next=bigStart;
		bigEnd=bigEnd==null?equalEnd:bigEnd;
	}
	return smallStart!=null?smallStart:equalStart!=null?equalStart:bigStart;
}

7.两个单链表相交的一系列问题

【题目】 在本题中,单链表可能有环,也可能无环。给定两个 单链表的头节点 head1和head2,这两个链表可能相交,也可能 不相交。请实现一个函数, 如果两个链表相交,请返回相交的 第一个节点;如果不相交,返回null 即可。
要求:如果链表1 的长度为N,链表2的长度为M,时间复杂度请达到 O(N+M),额外 空间复杂度请达到O(1)。

这道题算是链表中的难题。

思路: 小问题拆解

1、判断单链表有环或无环,有环则返回入环的节点,无环则返回null
两个思路

1.1 用hashset(hashset只有K,没有value) 依次存入hashset,返回成环节点

1.2 用两个指针,快指针一次两步,慢指针一次一步;如果快指针遇到空,直接无环;只要有环,一定相遇;此时,快指针回到开头,也一次走一步,两个指针必然在成环处相遇(结论,可以数学证明)

2、查找两个不成环的单链表相交的节点
两个思路

2.1 新建一个hashmap,把链表1放入map里,然后遍历链表2,看能否在map里查到,查到相交,否则不相交

2.2 不用哈希表,遍历两个链表,得到每个链表的长度len1、len2和最后的节点end1和end2;首先判断end1或者end2是否相等;相等的话代表必然相交,则将len大的链表从head处前进至小的两个len之差的位置,然后再依次往后比较,找相交节点。 若两个end不相等,则必然不相交。 注意:由于是单链表,所以不可能相交叉开

3、一个链表有环,一个链表——不可能相交 (单链表)

4、两个成环的单链表是否相交(只有三种情况)

第一种是不相交;第二种是先相交,再成环;第三种是各自进环。

思路:比较入环点loop1和loop2,如果相等就是第二种情况;不相等可能是1、3种情况,这时让loop1继续往下走,如果转回了loop1,则是第一种情况不相交,如果转到loop2,则相交,返回loop1或loop2都行。

对第二种,其实可以回溯到上方的两个不成环链表相交,只不过之前的end节点需要换成各自入环的loop节点即可。

代码如下:

public static Node getIntersectNode(Node head1,Node head2){
	if(head1==null||head2==null){
		return null;
	}
	Node loop1=getLoopNode(head1);
	Node loop2=getLoopNode(head2);
	if(loop1==null&&loopw==null){
		return noLoop(head1,head2);
	}
	if(loop1!=null&&loop2!=null){
		return bothLoop(head1,loop1,head2,loop2);
	}
	return null;
}
//判断成环函数(没有用到哈希函数)
public static Node getLoopNode(Node head){
	if(head==null||head.next==null||head.next.next==null){
			return null;
	}
	Node n1=head.next;
	Node n2=head.next.next;
	while(n1!=n2){
		if(n2.next==null||n2.next.next==null){
			return null;
		}
		n2=n2.next.next;
		n1=n1.next;
	}
	n2=head;
	while(n1!=n2){
		n1=n1.next;
		n2=n2.next;
	}
	return n1;	
}
//无环链表判断相交函数
public static Node noLoop(Node head1,Node head2){
	if(head1==null||head2==null){
		return null;
	}
	Node cur1=head1;
	Node cur2=head2;
	int n=0;
	while(cur1.next!=null){
		n++;
		cur1=cur1.next;
	}
	while(cur2.next!=null){
		n--;
		cur2=cur2.next;
	}
	if(cur1!=cur2){
		return null;
	}
	cur1=n>0?head1:head2;
	cur2=cur1==head1?head2:head1;
	n=Math.abs(n);
	while(n!=0){
		n--;
		cur1=cur1.next;
	}
	while(cur1!=cur2){
		cur1=cur1.next;
		cur2=cur2.next;
	}
	return cur1;
}
//判断成环链表是否相交
public static Node bothLoop(Node head1,Node loop1,Node head2,Node loop2){
	Node cur1=null;
	Node cur2=null;
	if(loop1==loop2){
		cur1=head1;
		cur2=head2;
		int n=0;
		while(cur1!=loop1){
			n++;
			cur1=cur1.next;
		}
		while(cur2!=loop2){
			n--;
			cur2=cur2.next;
		}
		cur1=n>0?head1:head2;
		cur2=cur1==head1?head2:head1;
		n=Math.abs(n);
		while(n!=0){
			n--;
			cur1=cur1.next;
		}
		while(cur1!=cur2){
			cur1=cur1.next;
			cur2=cur2.next;
		}
		return cur1;
	}
	else{
		cur1=loop1.next;
		while(cur1!=loop1.next){
			if(cur1==loop2){
				return loop1;
			}
			cur1=cur1.next;
		}
		return null;	
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值