当前位置:网站首页>[interview brush 101] linked list
[interview brush 101] linked list
2022-07-01 08:57:00 【hhhSir'blog】
List of articles
Abstract
【 Interview must brush 101】 series blog The purpose is to summarize the interview must brush 101 Interesting in 、 Exercises that may be tested in the interview . Summarize the general problem-solving methods , Summarize ideas for special exercises . It is written for your review , I also hope to communicate with you .
【 Interview must brush 101】 recursive / Backtracking algorithm summary I( Ten minutes to understand the backtracking algorithm )
【 Interview must brush 101】 recursive / Backtracking algorithm summary II( Ten minute backtracking algorithm problem )
1 Basic knowledge of linked list
Single chain list
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
Double linked list
public class ListNode {
int val;
ListNode next = null;
ListNode pre = null;
ListNode(int val) {
this.val = val;
}
}
There are few problems like linked list , Just pay attention to the following questions .
2 The interview must brush exercises
2.1 k A set of reverse linked list
Common reverse linked list :
A very simple picture , It refers to the current node to the previous node , Then turn the current node into the next node to traverse .
import java.util.*;
public class Solution {
public class Solution {
public ListNode ReverseList(ListNode cur) {
ListNode pre = null;
ListNode next = null;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
Then look at the linked list Every time k A group of nodes is flipped .

First, it can be realized by recursion : Every time k In fact, a group can do the latter first and then the front , Big problems consist of many small problems .
Then small problems can be solved by simply flipping the linked list . See code for details , It's worth learning .
import java.util.*;
public class Solution {
public ListNode reverseKGroup (ListNode head, int k) {
if (head == null || head.next == null) return head;
ListNode tail = head;
for (int i = 0; i < k; i++) {
if (tail == null) {
return head;
}
tail = tail.next;
}
// Before turning over k Elements
ListNode newHead = reverse(head, tail);
// head It's the last element . This recursion is very spiritual .
head.next = reverseKGroup(tail, k);
return newHead;
}
// reverse [head, tail)
public ListNode reverse (ListNode cur, ListNode tail) {
ListNode pre = null;
ListNode next = null;
while (cur != tail) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
2.2 Merge k A linked list
Topic link :BM5 Merge k Sorted linked list 
Why is this problem worth doing ? There are three main inspection points :
- What is the merger strategy ? be based on Merge sort Two by two consolidation of ; Priority queue .
- How to merge two ordered linked lists ? Build a Head node , Insert whoever is young .
import java.util.*;
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
return mergeList(lists, 0, lists.size() - 1);
}
public ListNode mergeList(ArrayList<ListNode> list, int l, int r) {
if (l== r) {
return list.get(l);
}
if (l > r) {
return null;
}
int mid = l + ((r - l) >> 1);
ListNode left = mergeList(list, l, mid);
ListNode right = mergeList(list, mid + 1, r);
return merge2Lists(left, right);
}
// Merge two linked lists
public ListNode merge2Lists(ListNode list1, ListNode list2) {
ListNode head = new ListNode(0);
ListNode cur = head;
while (list1 != null && list2 != null) {
if (list1.val > list2.val) {
cur.next = list2;
list2 = list2.next;
} else {
cur.next = list1;
list1 = list1.next;
}
cur = cur.next;
}
if (list1 != null) cur.next = list1;
if (list2 != null) cur.next = list2;
return head.next;
}
}
2.3 Add the linked list
Topic link : Add the linked list ( Two )
At first glance, it seems so difficult ? Where to start adding ? Unclear . Why not , Because from next Node Can't find pre Node, This is the characteristic of single linked list . however , If you turn it over, you can do it well .
import java.util.*;
/* * public class ListNode { * int val; * ListNode next = null; * } */
public class Solution {
/** * * @param head1 ListNode class * @param head2 ListNode class * @return ListNode class */
public ListNode addInList (ListNode head1, ListNode head2) {
ListNode h1 = reverse(head1);
ListNode h2 = reverse(head2);
int tag = 0;
int val = 0;
ListNode cur = null, pre = null;
while (h1 != null || h2 != null) {
if (h1 != null) {
val += h1.val;
h1 = h1.next;
}
if (h2 != null) {
val += h2.val;
h2 = h2.next;
}
val += tag;
if (val >= 10) {
tag = 1;
val = val % 10;
} else {
tag = 0;
}
cur = new ListNode(val);
val = 0;
cur.next = pre;
pre = cur;
}
if (tag == 1) {
cur = new ListNode(1);
cur.next = pre;
}
return cur;
}
public ListNode reverse (ListNode cur) {
ListNode pre = null;
ListNode next = null;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
2.4 The sort of single linked list
Topic link :BM12 The sort of single linked list .
2.5 Judge whether a linked list is palindrome structure
2.6 Delete duplicate elements of linked list
边栏推荐
- Shell脚本-echo命令 转义符
- 3、Modbus通讯协议详解
- pcl_viewer命令
- 个人装修笔记
- [MFC development (17)] advanced list control list control
- factory type_ Id:: create process resolution
- Shell script case in statement
- Screenshot tips
- 【MFC开发(17)】高级列表控件List Control
- What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
猜你喜欢

FreeRTOS learning easy notes

Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

Pain points and solutions of equipment management in large factories

Bimianhongfu queren()

Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched

How to manage fixed assets efficiently in one stop?

大型工厂设备管理痛点和解决方案

内存大小端

如何做好固定资产管理?易点易动提供智能化方案
随机推荐
3. Detailed explanation of Modbus communication protocol
What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
安装Oracle EE
It technology ebook collection
电脑小技巧
Shell脚本-echo命令 转义符
Shell script -read command: read data entered from the keyboard
AVL树的理解和实现
毕业季,我想对你说
Redis源码学习(29),压缩列表学习,ziplist.c(二)
Foundation: 2 The essence of image
嵌入式工程师面试-常问问题集
V79.01 Hongmeng kernel source code analysis (user mode locking) | how to use the fast lock futex (Part 1) | hundreds of blogs analyze the openharmony source code
"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis
Shell script - array definition and getting array elements
Football and basketball game score live broadcast platform source code /app development and construction project
基础:3.opencv快速入门图像和视频
【MFC开发(16)】树形控件Tree Control
钓鱼识别app
Shell脚本-select in循环