当前位置:网站首页>[classic example] classic list questions @ list
[classic example] classic list questions @ list
2022-07-01 11:45:00 【Destiny Officer】
I believe you will encounter the same situation as me when you first learn data structure. Problems cannot constitute effective ideas .
Through continuous practice, I believe we will also become big guys , It will also be taken to the big factory offer.
List sorting
First of all, we know the idea of solving this problem according to the topic It will probably use ① Double pointer ② List sorting
1. Set up fast and slow Two pointers use fast Take two steps slow The principle of taking one step will slow Set as the intermediate node of the linked list , And then pre Pointer to slow Pointer to the previous node . So now we will pre Pointer to a null, You can divide the original linked list into two parts .
2. Sort the two-part linked list . In order to complete the next action .
3. To this step The two parts of the linked list are in good order , Create a new node header Compare The value of the two chain header nodes , Use the new linked list node to connect with the party with small value .
4. Complete all steps Just return to the set node .
class Solution {
public ListNode sortList(ListNode head) {
return mergeSort(head); }
ListNode mergeSort(ListNode head){
if(head == null || head.next == null) return head;
ListNode fast = head;
ListNode slow = head;
ListNode pre = null;
while(fast != null && fast.next != null){// Use the speed pointer to find the middle node of the linked list
pre = slow;// here pre stay last In the previous position
fast = fast.next.next;
slow = slow.next;
}
pre.next = null;// bring pre Of next It's empty In this way, the linked list is divided into two parts Part in head start Part in slow start
ListNode l1 = mergeSort(head); Use merge sort to sort the two parts of the linked list
ListNode l2 = mergeSort(slow);
return merge(l1, l2);
}
ListNode merge(ListNode l1, ListNode l2){
ListNode newhead = new ListNode(0); Create a new chain header
ListNode cur3 = newhead;
while(l1 != null && l2 != null){
if(l1.val < l2.val){ Compare the value of the two linked lists
cur3.next = l1; use cur3 Connect
l1 = l1.next;
}else{
cur3.next = l2;
l2 = l2.next;
}
cur3 = cur3.next;
}
if(l1 != null){
cur3.next = l1;
}
if(l2 != null){
cur3.next = l2;
}
return newhead.next;
}
}
Delete duplicate elements from the sort list
Their thinking
As usual, we should judge the special situation first
Create a new node connector node The use of l2 To adjudicate see l2.val Values and l2.next.val Are they the same? .
This step introduces you
As we enter if In the loop On behalf of l2.val == l2.next.val When we while Walk the It means that the current value is different from the next value
At this time, we are still if Inside In order to prevent it from being followed by the same set of values We need to continue this sentence of continuous judgment in the cycle .
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null || head.next == null) return head;
ListNode newhere = new ListNode (0); Create a new node
newhere.next = head;
ListNode l1 = newhere;
ListNode l2 = head;
while(l2 != null){ The loop condition l2 Can't be empty
if(l2.next != null && l2.val == l2.next.val){ Now judge l2 Of val and l2.next Of val Are they the same?
while(l2.next != null && l2.val == l2.next.val){ This step is crucial Will explain clearly in the problem-solving ideas
l2 = l2.next;
}
l1.next = l2.next;
l2 = l1.next;
}else{
l1 = l2;
l2 = l2.next;
}
}
return newhere.next;
}
}
Rearrange the list
Understand the meaning of the question Is to connect the first and last The second one is connected with the penultimate one In turn Until the position of the middle most element
Use the speed pointer to operate Find the middle element It is divided into two linked list parts Reverse the second half Make the last element become the first element, and then merge .
class Solution {
public void reorderList(ListNode head) {
ListNode fast = head.next;
ListNode slow = head;
while(fast != null && fast.next != null){ The speed pointer operates
fast = fast.next.next;
slow = slow.next;
}
ListNode pre = slow.next;
ListNode cur1 = null;
slow.next = null;
while(pre != null){ Reverse linked list operation
ListNode cur = pre.next;
pre.next = cur1;
cur1 = pre;
pre = cur;
}
ListNode cur3 = head;
ListNode l3 = cur1;
while(cur3 != null && l3 != null){
ListNode l1 = cur3.next; Merge two linked lists
ListNode l2 = l3.next;
cur3.next = l3;
cur3 = l1;
l3.next = cur3;
l3 = l2;
}
}
}
Delete continuous nodes with a total value of zero from the linked list
Their thinking
Continue to traverse nodes and add them together Operate when it is judged to be zero .
class Solution {
public ListNode removeZeroSumSublists(ListNode head) {
ListNode pre = new ListNode(0);
ListNode cur =pre;
pre.next = head;
while(pre != null){
ListNode cur1 = pre.next;
int sum = 0;
while(cur1 != null){ When the sum of calculation is zero
sum += cur1.val;
cur1 = cur1.next;
if(sum == 0){ When you find what you need
pre.next = cur1; To delete
break;
}
}
if(cur1 == null) pre = pre.next;
}
return cur.next;
}
}
Reverse a linked list
When we see this question, we will think that we should find it first right The next node and left The previous node . This is convenient for us to reverse . And so it is
class Solution {
public ListNode reverseBetween(ListNode head, int left, int right) {
ListNode node = new ListNode(0);
node.next = head;
ListNode pre = node;
for(int i = 1; i < left; i ++){ First find left The previous node
pre = pre.next;
}
ListNode haed = pre.next; take haed Insert the first element to reverse
for(int j = left; j < right; j ++ ){ The following part is reversed
ListNode nex = haed.next;
haed.next = nex.next;
nex.next = pre.next;
pre.next = nex;
}
return node.next;
} Finally, output the header node
}
I believe tomorrow will be better !!!🤭
边栏推荐
- Tempest HDMI leak reception 4
- How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
- 陈珙:微服务,它还那么纯粹吗?
- 力扣首页简介动画
- 超详细黑苹果安装图文教程送EFI配置合集及系统
- Learning summary on June 28, 2022
- How to set decimal places in CAD
- 为什么一定要从DevOps走向BizDevOps?
- Redis startup and library entry
- 优雅地翻转数组
猜你喜欢
Abbirb120 industrial robot mechanical zero position
商汤进入解禁期:核心管理层自愿禁售 强化公司长期价值信心
CPI tutorial - asynchronous interface creation and use
博途V15添加GSD文件
How to understand the developed query statements
Unittest 框架介绍及第一个demo
Tempest HDMI leak receive 5
Learning summary on June 30, 2022
Matrix of numpy
About keil compiler, "file has been changed outside the editor, reload?" Solutions for
随机推荐
证券账户销户后果 开户安全吗
Unittest 框架介绍及第一个demo
Test case writing specification in unittest framework and how to run test cases
耐克如何常年霸榜第一名?最新财报答案来了
241. Design priority for operational expressions: DFS application questions
Continuous delivery -pipeline getting started
Skip the test cases to be executed in the unittest framework
使用set_handler过滤掉特定的SystemC Wraning &Error Message
用于分类任务的数据集划分脚本
妙啊!MarkBERT
How to set decimal places in CAD
对于mvvm和mvc的理解
Want to ask, is there a discount for opening a securities account? Is it safe to open a mobile account?
Openinstall: wechat applet jump to H5 configuration service domain name tutorial
TEMPEST HDMI泄漏接收 5
ABBIRB120工业机器人机械零点位置
JS日期格式化转换方法
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
S7-1500PLC仿真
华为HMS Core携手超图为三维GIS注入新动能