01刷题打卡-BM1~4

56 阅读1分钟

面试必刷TOP101

[toc]

一、链表

1.1、BM1 反转链表

a. 题目

image.png

b. 思路

。。。

c. 解答

import java.util.*;

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

    ListNode(int val) {
        this.val = val;
    }
}*/

public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        
        while(cur!=null){
            LisNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;        
        }
        
        return pre;    
    }
}

1.2、BM2 链表内指定区间反转

a. 题目

下图的2,4 为第2个数、第4个数(初始为1) image.png

b. 思路

。。。

c. 解答

import java.util.*;

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

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param m int整型 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode reverseBetween (ListNode head, int m, int n) {
        ListNode res = new ListNode(-1);
        res.next = head;
        ListNode pre = res;
        ListNode cur = head;
        
        for(int i=1;i<m;i++){
            pre = cur;
            cur = cur.next;
        }
        
        for(int i=m;i<n;i++){
            ListNode next = cur.next;
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }       
        
        return res.next; 
    }    
}

1.3、BM3 链表中的节点每k个一组翻转

a. 题目

image.png

b. 思路

。。。

c. 解答

import java.util.*;

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

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    public ListNode reverseKGroup (ListNode head, int k) {
        ListNode tail = head;
        
        for(int i=0;i<k;i++){
            if(tail==null) return head;
            tail = tail.next;
        }
        
        ListNode pre = null;
        ListNode cur = head;
        
        while(cur!=tail){
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;;
            cur = next;
        }
        
        head.next = reverseKGroup(tail,k);
        
        return pre;        
    }
}

1.4、BM4 合并两个排序的链表

a. 题目

image.png

b. 思路

。。。

c. 解答

import java.util.*;
/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode Merge(ListNode list1,ListNode list2) {
        ListNode p1 = list1;
        ListNode p2 = list2;
        ListNode res = new ListNode(-1);
        ListNode cur = res;
        
        while( p1 !=null && p2 !=null ){
            if(p1.val<p2.val){
                cur.next = p1;
                cur = cur.next;
                p1 = p1.next;
            }else{
                cur.next = p2;
                cur = cur.next;
                p2 = p2.next;
            }        
        }
        
        if(p1!=null){
            cur.next = p1;
        }else{
            cur.next = p2;
        }
        
        return res.next;         
    }
}