<<<<<<< HEAD
title: 百度一面总结
cover: https://www.baidu.com/img/flexible/logo/pc/result.png
单例模式的写法
饿汉式
1 | public class Main{ |
懒汉式
1 | public class Main{ |
双重检测的单例模式
1 | public class Main{ |
算法
题目
给定一个单链表 L
的头节点 head
,单链表 L
表示为:
image-20220725185720810
请将其重新排列后变为:
image-20220725185702653
不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。原地交换
1 | /** |
public class Main{
private static Main main = new Main();
private Main(){
}
public static Main getInstance(){
return main;
}
}
JAVA
1 |
|
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 |
|
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 |
|
/**
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
```