极客时间算法课笔记整理2——理论讲解+面试题实战:数组、链表

数组、链表(Array、Linked List)

数组

结构

内存里一段连续的存储区域
在这里插入图片描述

查取插入删除

在这里插入图片描述

在这里插入图片描述
为了改善插入删除,提出了

链表

在这里插入图片描述
适用于

  1. 插入删除操作很多
  2. 不知道有多少元素

单链表

变形,多了头指针和尾指针
在这里插入图片描述

插入

在这里插入图片描述

删除

在这里插入图片描述

时间复杂度

查找O(n)
插入删除O(1)

双链表

在这里插入图片描述

在这里插入图片描述

面试题

在这里插入图片描述

206. Reverse Linked List

  1. Reverse Linked List
    Easy

Reverse a singly linked list.

Example:

Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

自己的解题方法一:迭代

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null){return null;}
        ListNode next,tmp,curr;
        curr=head;
        next = head.next;
        head.next=null;
        while(next!=null){
            tmp=next.next;
            next.next=curr;
            curr=next;
            next=tmp;
        }
        return curr;
    }
}

结果:
在这里插入图片描述

自己的解题方法二:递归

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null){return null;}
        ListNode curr, next;
        curr = head;
        next = head.next;
        head.next=null;
        return reverse(curr,next);
    }
    
    public ListNode reverse(ListNode curr, ListNode next){
        ListNode tmp;
        if(next!=null){
            tmp=next.next;
            next.next=curr;
            curr=next;
            next=tmp;
            return reverse(curr,next);
        }
        else{
            return curr;
        }
    }
}

结果:
在这里插入图片描述
老师解法:
在这里插入图片描述

转换为Java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode cur,prev,tmp;
        cur= head;
        prev = null;
        while(cur!=null){
            tmp=cur.next;
            cur.next=prev;
            prev=cur;
            cur=tmp;
        }
        return prev;
    }
}

结果:
在这里插入图片描述
改进点:将边缘案例包含在初始化条件中

24. Swap Nodes in Pairs

在这里插入图片描述
注意:JAVA的“=”是传递的地址!参见:
在这里插入图片描述
在这里插入图片描述
自己的解法:

class Solution {
    public ListNode swapPairs(ListNode head) {
         
        while(cur!= null){
        if(cur.next==null || cur.next.next==null){
            pre.next=cur.next;
            cur.next=pre;
            return res;
        }else{
            pre.next=cur.next.next;
            tmp=pre;
            pre=cur.next;            ;
            cur.next=tmp;
            cur=pre.next;
        }
        }
        return res;
}
}

结果:
在这里插入图片描述
老师的解法:
在这里插入图片描述

141. Linked List Cycle

自己的解法1:
注意:Map的参数是reference类型,比如int会报错??,修改为integer。
Java中Map的使用

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ListNode cur=head;
        Integer pos=0;
        Map<ListNode,Integer> map=new HashMap<ListNode, Integer>();
        while(cur!=null){
            if(map.containsKey(cur)){pos=map.get(cur);return true;}
            else{
            map.put(cur,pos);
            pos=pos+1;
            cur=cur.next;}
        }
        return false;
    }
}

结果:
在这里插入图片描述
老师的方法:
在这里插入图片描述
在这里插入图片描述

142. Linked List Cycle II

自己的解法:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode detectCycle(ListNode head) {
        ListNode cur=head;
        Integer pos=0;
        Map<ListNode,Integer> map=new HashMap<ListNode, Integer>();
        while(cur!=null){
            if(map.containsKey(cur)){pos=map.get(cur);return cur;}
            else{
            map.put(cur,pos);
            pos=pos+1;
            cur=cur.next;}
        }
        return null;
    }
}

结果:
在这里插入图片描述

25. Reverse Nodes in k-Group

自己的解法:

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode cur,pre,self,a,b,tmp,stop;
        cur=head;
        self=head;
        a=cur;
        pre=null;
        stop=new ListNode();
        for(int i=0;i<k;i++){
                if(head!=null){ 
                    self=head;
                    head=head.next;
                }else{
                    self=cur;
                }
        }
        while(cur!=null){
            for(int i=0;i<k;i++){
                if(cur!=null){
                    pre=cur;
                    cur=cur.next;
                }else{
                    return self;
                }
            }
            stop.next=pre;
            b=cur;
            stop=a;
            for(int i=0;i<k;i++){
                tmp=a.next;
                a.next=b;
                b=a;
                a=tmp;
            }
        }        
        return self;
    }
}

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值