数据结构笔记三
前言
内容主要是链表
1.单链表题目
第一题
方案一:使用栈,遍历一遍全部进栈。然后重新遍历每遍历一个结点就从栈里弹出一个结点,遍历过程中如果每一个结点都相等,就是回文
方案二:还是用栈,但是省一点空间,只把后半的内容进栈,使用快慢数组得出后半的链表结点,节省了一半的空间
方案三:使用快慢指针,当快指针走到结尾时,慢指针走到中间,这时将中间结点指向空,然后把后半部分逆序,最后从头往中间走,同时从后往中间走,每走一步都把值做比较,比较完了将链表恢复原样。不使用额外的空间
三个方案的代码如下:
public static boolean Palindrome1(Node head){
if (head==null)return false;
Node p=head;
Stack<Node> stack=new Stack<>();
while (p!=null){
stack.add(p);
p=p.next;
}
while (!stack.isEmpty()){
if (head.val!=stack.pop().val) return false;
head=head.next;
}
return true;
}
public static boolean Palindrome2(Node head){
if (head==null)return false;
if(head.next==null)return true;
Node p1=head,p2=head;
while(p2.next.next!=null){//当链表只有俩个节点和一个时,快指针第一步就走不了
p1=p1.next;
p2=p2.next.next;
if(p2.next==null)break;
}//当出现快指针一步要走两个结点但是已经没有结点可走时,处理方法是就不走了
//因为快指针后面只存在有一个结点和无节点可走的情况
p2=p1;
Stack<Node>stack=new Stack<>();
while(p2.next!=null){
p2=p2.next;
stack.add(p2);
}
while (!stack.isEmpty()){
if(head.val!=stack.pop().val) return false;
head=head.next;
}
return true;
}
public static boolean Palindrome3(Node head){//我是选择按照链表长度奇数和偶数不同的处理,确实写的麻烦
if (head==null)return false;
if(head.next==null)return true;
Node p1=head,p2=head;
while(p2.next.next!=null){
p1=p1.next;
p2=p2.next.next;
if(p2.next==null)break;
}
Node tem=null;
if(p2.next!=null){//说明链表的长度是偶数,奇偶的代码不相同的只有一步,左神的代码要更为简洁和优化
p2=p2.next;
Node p3=p1.next,p4=p3.next;//开始反转链表
p3.next=null;
while (p4!=p2){
tem=p4.next;
p4.next=p3;
p3=p4;
p4=tem;
}
p4.next=p3;
while (p3!=null){//一边验证一边将链表恢复原状
if(head.val!=p2.val) return false;
head=head.next;
tem=p3.next;
p3.next=p2;
p2=p3;
p3=tem;
}
if(head.val!=p2.val) return false;
p4.next=null;
}
else {//说明链表的长度是奇数
Node p3=p1,p4=p3.next;//开始反转链表
p3.next=null;
while (p4!=p2){
tem=p4.next;
p4.next=p3;
p3=p4;
p4=tem;
}
p4.next=p3;
while (p3!=null){//一边验证一边将链表恢复原状
if(head.val!=p2.val) return false;
head=head.next;
tem=p3.next;
p3.next=p2;
p2=p3;
p3=tem;
}
if(head.val!=p2.val) return false;
p4.next=null;
}
return true;
}
第二题
代码如下:
解法就是建立三个链表一个是小于,一个是等于,一个是大于,但是都要记录头和尾,遍历链表的过程中将三个链表补充完整,最后将三个链表串起来就完成了。
public static Node ListPartition(Node head,int pivot){
Node Sh=null,St=null,Eh=null,Et=null,Gh=null,Gt=null;//三组链表的头和尾
while (head!=null){
if(head.val==pivot) {
if(Eh==null){
Eh=head;
Et=head;
}
else {
Et.next=head;
Et=head;
}
}
else if(head.val<pivot) {
if(Sh==null){
Sh=head;
St=head;
}
else {
St.next=head;
St=head;
}
}
else {
if(Gh==null){
Gh=head;
Gt=head;
}
else {
Gt.next=head;
Gt=head;
}
};
head=head.next;
}//下面三个队列判断和链接的代码我是参照左神的写的,但是我写的也不好。。。
if(Sh!=null){//如果存在小于队列
St.next=Eh;//如果存在等于队列那就指向队列的头,如果不存在就为null满足要求
Et.next=null;
}
if(Gh!=null&&Eh!=null){//如果存在大于队列
Et.next=Gh;
Gt.next=null;
}
if(Gh!=null&&Eh==null){
if(Sh!=null){
St.next=Gh;
Gt.next=null;
}
}
return Sh!=null?Sh:(Eh!=null?Eh:Gh);
}
}
第三题
这题也是力扣的题目
https://leetcode.cn/problems/fu-za-lian-biao-de-fu-zhi-lcof/
代码如下:
public Node copyRandomList(Node head) {
if(head==null) return head;
Node p1=head;//储存头节点
Node p2=null;
while (head!=null){//第一次循环,在每一个节点的后面创建一个该节点的复制
Node tem=new Node(head.val);
tem.next=head.next;
head.next=tem;
head=tem.next;
}
head=p1;//指针再次回到头部
while (head!=null) {//第二次循环复制random指针
Node tem = head.next;
p2 = head.random;
if (p2 == null) tem.random = null;
else tem.random = p2.next;
head = tem.next;
}
head=p1;
p1=head.next;
while (head!=null){//第三次循环将复制的链表从原链表中取出,并且将原来的链表回复原状
Node tem=head.next;
if (head.next==null) break;
head.next=head.next.next;
head=tem;
}
return p1;
}
第四题
代码如下:
//首先是判断链表是否有环
public ListNode detectCycle(ListNode head) {//寻找有环链表的入环节点
if(head==null)return null;
ListNode fast=head,slow=head;//定义快慢指针
if(fast.next!=null){//快慢指针都走一次,不然第一步就是相遇的
fast=fast.next.next;
slow=slow.next;
}
else return null;
while (slow!=fast){
if(fast==null||fast.next==null)return null;
fast=fast.next.next;
slow=slow.next;
}
fast=head;//快指针返回头,然后两个指针都开始一步一步的走
while (slow!=fast){//不需要在判断是否为空,因为运行到这里一定有环
fast=fast.next;
slow=slow.next;
}
return slow;
}
//如果两个链表都无环就是简单的单链表找第一个公共结点
public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null)return null;
int lena=0,lenb=0;//记录两个链表的长度
ListNode pa=headA,pb=headB;
while (pa.next!=null){//记录a链表走了几步
pa=pa.next;
lena++;
}
while (pb.next!=null){//记录b链表走了几步
pb=pb.next;
lenb++;
}
if(pa!=pb)return null;//如果两个链表的最后一个结点不相同,那么这两个结点不相交
pa=headA;
pb=headB;
if(lena>lenb){//将两个链表缩进,使从指针开始的位置到结尾的距离对于两个链表都相同,简单来说就是a链表长4,b链表长5,那么b就先走一步,然后a,b同时开始走
for(int i=lena-lenb;i>0;i--){
pa=pa.next;
}
}
if ((lena < lenb)) {
for(int i=lenb-lena;i>0;i--){
pb=pb.next;
}
}
while (pa!=pb){
pa=pa.next;
pb=pb.next;
}
return pa;
}
public ListNode Bothloop(ListNode headA, ListNode headB){//如果两个链表都有环
ListNode a=detectCycle(headA);
ListNode b=detectCycle(headB);
ListNode c=headA;
if(a==b){//入环结点相同那么说明两个链表交点在入环结点或者入环结点之前,我们只需要把环无视掉,再用无环结点的方法判断即可
ListNode next=a.next;
a.next=null;
ListNode tem=getIntersectionNode(a,b);
a.next=next;
return tem;
}
else {//接下来的情况就是a、b完全不相交,或者两链表共用一个环
headA=headA.next;
while (headA!=c){//a自个绕一圈,绕圈的路上要是遇上了b那么就是两链表共用一个环,如果遇不上那么就是完全不相交
if(headA==headB){
return headA;
}
}
return null;
}
}
那么后面的内容就是二叉树了。