链表3+栈和队列2

1. 链表中环的入口结点

题目描述 :
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。

链接:https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4
来源:牛客网
思路:
第一步,找环中相汇点。分别用p1,p2指向链表头部,p1每次走一步,p2每次走二步,直到p1== p2找到在环中的相汇点。
第二步,找环的入口。接上步,当p1== p2时,p2所经过节点数为2x,p1所经过节点数为x,设环中有n个节点,p2比p1多走一圈有2x=n+x; n=x;可以看出p1实际走了一个环的步数,再让p2指向链表头部,p1位置不变,p1,p2每次走一步直到p1==p2; 此时p1指向环的入口。

链接:https://www.nowcoder.com/questionTerminal/253d2c59ec3e4bc68da16833f79a38e4
来源:牛客网

public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if(pHead==null||pHead.next==null) return null;
        ListNode slow=pHead;
        ListNode fast=pHead;
        while(fast!=null&&fast.next!=null){
                slow=slow.next;
                fast=fast.next.next;
                if(slow==fast){
                    slow=pHead;
                    while(slow!=fast){
                        slow=slow.next;
                        fast=fast.next;
                    }
                    if(slow==fast) return slow;
            }
        }
        return null;
    }
}
/*
 public class ListNode {
    int val;
    ListNode next = null;

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

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

2. 删除链表中重复的结点

题目描述:
在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。 例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

链接:https://www.nowcoder.com/questionTerminal/fc533c45b73a41b0b44ccba763f866ef
来源:牛客网


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

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
        ListNode first=new ListNode(-1);
        first.next=pHead;
        ListNode p=pHead;
        ListNode q=first;
        while(p!=null&&p.next!=null){
            if(p.val==p.next.val){
                int val=p.val;
                while(p!=null&&p.val==val)
                    p=p.next;
                q.next=p;
            }
            else{
                p=p.next;
                q=q.next;
            }
        }
        return first.next;
    }
}

3. 从尾到头打印链表

题目描述
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。


import java.util.ArrayList;
public class Solution {
    ArrayList<Integer> arraylist =new ArrayList<>();
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        if(listNode!=null){
            printListFromTailToHead(listNode.next);
            arraylist.add(listNode.val);
        }
        return arraylist;
    }
}




import java.util.Stack;
import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        Stack<Integer> stack =new Stack<>();
        ArrayList<Integer> arraylist=new ArrayList<>();
        ListNode p=listNode;
        while(p!=null){
            stack.push(p.val);
            p=p.next;
        }
        while(!stack.empty()){
            arraylist.add(stack.pop());
        }
        return arraylist;
    }
}

1. 滑动窗口的最大值

题目描述
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。

//小根堆
Queue<Integer> little =new PriorityQueue<Integer>();

//大根堆
Queue<Integer> bige new PriorityQueue<Integer>(11,new Comparator<Integer>(){
    @override
    public int compare(Integer o1,Integer o2){
        return o2.compareTo(o1);
    }
});

//大根堆
Queue<Integer> big =new PriorityQueue<Integer>(11,new Comparator<Integer>(){
    @override
    public  int compare(Integer o1,Integer o2){
        return o2-o1;
    }
});

 Queue<Integer> big= new PriorityQueue<Integer>(new Comparator<Integer>(){
        @Override
        public int compare(Integer i1, Integer i2){
            return Integer.compare(i2, i1);
        }
    });
    
import java.util.ArrayList;
import java.util.ArrayDeque;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size)
    {
        ArrayList<Integer> arraylist =new ArrayList<>();
        if(num.length==0||size<=0) return arraylist;
        ArrayDeque<Integer> deque =new ArrayDeque<>();
        for(int i=0;i<num.length;i++){
            while(!deque.isEmpty()&&deque.peekFirst()<i-size+1){
                deque.pollFirst();
            }
            while(!deque.isEmpty()&&num[deque.peekLast()]<num[i]){
                deque.pollLast();
            }
            deque.offer(i);
            if(i>=size-1)arraylist.add(num[deque.peekFirst()]);
        }
        return arraylist;
    }
}
import java.util.ArrayList;
import java.util.Queue;
import java.util.PriorityQueue;
import java.util.Comparator;
public class Solution {
    public ArrayList<Integer> maxInWindows(int [] num, int size)
    {
        ArrayList<Integer> arraylist = new ArrayList<>();
        if(num.length==0||size<=0|| num.length<size) return arraylist;
        Queue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>(){
            @Override
            public int compare(Integer o1,Integer o2){
                return o2.compareTo(o1);
            }
        });
        for(int i=0;i<size;i++){
            queue.add(num[i]);
        }
        arraylist.add(queue.peek());
        for(int i=size;i<num.length;i++){
            queue.remove(num[i-size]);
            queue.add(num[i]);
            arraylist.add(queue.peek());
        }
        return arraylist;
    }
}

2. 用两个栈实现队列

题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        stack1.push(node);
    }
    
    public int pop() {
        if(stack1.empty()&&stack2.empty()) throw new RuntimeException("Queue is empty!");
            if(stack2.empty()){
                while(!stack1.empty()){
                    stack2.push(stack1.pop());
                }
            }
        return stack2.pop();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值