当前位置:网站首页>[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
边栏推荐
- Redis——Lettuce连接redis集群
- Introduction to 18mnmo4-5 steel plate executive standard and delivery status of 18mnmo4-5 steel plate, European standard steel plate 18mnmo4-5 fixed rolling
- 日常办公耗材管理解决方案
- Phishing identification app
- 中考体育项目满分标准(深圳、安徽、湖北)
- Yolov3, 4, 5 and 6 Summary of target detection
- Matlab tips (23) matrix analysis -- simulated annealing
- Nacos - 配置管理
- Bimianhongfu queren()
- 美团2022年机试
猜你喜欢

Matlab tips (23) matrix analysis -- simulated annealing

中小企业固定资产管理办法哪种好?

Jetson Nano 安装TensorFlow GPU及问题解决

Glitch free clock switching technology
![[MFC development (17)] advanced list control list control](/img/e8/24c52cb51defc6c96b43c2ef3232ff.png)
[MFC development (17)] advanced list control list control

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

How to manage fixed assets efficiently in one stop?

Which method is good for the management of fixed assets of small and medium-sized enterprises?

1.jetson与摄像头的对接

ARM v7的体系结构A、R、M区别,分别应用在什么领域?
随机推荐
Shell脚本-变量的定义、赋值和删除
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
Shell script echo command escape character
Shell script -for loop and for int loop
Differences among tasks, threads and processes
中断与其他函数共享变量、临界资源的保护
Do you know how data is stored? (C integer and floating point)
TV size and viewing distance
How to manage fixed assets well? Easy to point and move to provide intelligent solutions
Nacos - service discovery
Shell脚本-字符串
公网集群对讲+GPS可视追踪|助力物流行业智能化管理调度
Pain points and solutions of equipment management in large factories
Shell script -read command: read data entered from the keyboard
中考体育项目满分标准(深圳、安徽、湖北)
1. Connection between Jetson and camera
Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
FreeRTOS学习简易笔记
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
Mysql 优化