少女祈祷中...

title: 百度一面总结
cover: https://www.baidu.com/img/flexible/logo/pc/result.png

单例模式的写法

饿汉式

1
2
3
4
5
6
7
8
9
10
public class Main{
private static Main main = new Main();
private Main(){
}
public static Main getInstance(){
return main;
}
}

JAVA

懒汉式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Main{
private static Main main;
private Main(){
}
public static synchronized Main getInstance(){
if(main == null){
return new Main();
}else{
return main;
}
}
}

JAVA

双重检测的单例模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Main{
private static volatile Main main;
private Main(){
}
public Main getInstance(){
if(main == null){
synchronized(Main.class){
if(main == null){
main = new Main();
}
}
}
return main;
}
}

JAVA

算法

题目

143. 重排链表 - 力扣(LeetCode)

给定一个单链表 L 的头节点 head ,单链表 L 表示为:

image-20220725185720810

image-20220725185720810

请将其重新排列后变为:

image-20220725185702653

image-20220725185702653

不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。原地交换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* 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 void reorderList(ListNode head) {
if(head == null){
return ;
}else{
ListNode mid = findMid(head);
ListNode relist = reverseList(mid.next);
mid.next = null;

ListNode copy = head;
ListNode recopy = relist;
//合并两个链表
while( recopy != null){
ListNode tmp = copy.next;
ListNode tmp1 = recopy.next;
copy.next = recopy;
recopy.next = tmp;
copy = tmp;
recopy = tmp1;
}

}

}

//快慢指针返回列表的中点
public ListNode findMid(ListNode head){
ListNode slow = head;
ListNode fast = head;

while(fast != null && fast.next != null){
slow = slow.next;
fast = fast.next.next;
}
return slow;
}


//反转链表
public ListNode reverseList(ListNode head){
if(head == null){
return null;
}else if(head.next == null){
return head;
}else{
ListNode node = reverseList(head.next);
head.next.next = head;
head.next = null;
return node;
}
}
}
=======
---
title: 百度一面总结
cover: https://www.baidu.com/img/flexible/logo/pc/result.png
---

## 单例模式的写法

### **饿汉式**

public class Main{
private static Main main = new Main();
private Main(){
}
public static Main getInstance(){
return main;
}
}

JAVA

1
2
3

### 懒汉式

public class Main{
private static Main main;
private Main(){
}
public static synchronized Main getInstance(){
if(main == null){
return new Main();
}else{
return main;
}
}
}

JAVA

1
2
3

### 双重检测的单例模式

public class Main{
private static volatile Main main;
private Main(){
}
public Main getInstance(){
if(main == null){
synchronized(Main.class){
if(main == null){
main = new Main();
}
}
}
return main;
}
}

JAVA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

## 算法

### 题目

[143. 重排链表 - 力扣(LeetCode)](https://leetcode.cn/problems/reorder-list/)

给定一个单链表 `L` 的头节点 `head` ,单链表 `L` 表示为:

[![image-20220725185720810](file:///C:/Users/jinhu/AppData/Roaming/Typora/typora-user-images/image-20220725185720810.png)](file:///C:/Users/jinhu/AppData/Roaming/Typora/typora-user-images/image-20220725185720810.png)

image-20220725185720810



请将其重新排列后变为:

[![image-20220725185702653](file:///C:/Users/jinhu/AppData/Roaming/Typora/typora-user-images/image-20220725185702653.png)](file:///C:/Users/jinhu/AppData/Roaming/Typora/typora-user-images/image-20220725185702653.png)

image-20220725185702653



不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。原地交换

/**

  • 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 void reorderList(ListNode head) {
    if(head == null){
    return ;
    }else{
    ListNode mid = findMid(head);
    ListNode relist = reverseList(mid.next);
    mid.next = null;

    ListNode copy = head;
    ListNode recopy = relist;
    //合并两个链表
    while( recopy != null){
    ListNode tmp = copy.next;
    ListNode tmp1 = recopy.next;
    copy.next = recopy;
    recopy.next = tmp;
    copy = tmp;
    recopy = tmp1;
    }

    }
    }

    //快慢指针返回列表的中点
    public ListNode findMid(ListNode head){
    ListNode slow = head;
    ListNode fast = head;

    while(fast != null && fast.next != null){
    slow = slow.next;
    fast = fast.next.next;
    }
    return slow;
    }

    //反转链表
    public ListNode reverseList(ListNode head){
    if(head == null){
    return null;
    }else if(head.next == null){
    return head;
    }else{
    ListNode node = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return node;
    }
    }

}

660263c04a65ea2551ce86cca85df1d3e5eb5ec1
```