当前位置:网站首页>Sword finger offer 18.22.25.52 Double pointer (simple)
Sword finger offer 18.22.25.52 Double pointer (simple)
2022-06-26 14:13:00 【hedgehog:】
18.
subject :
idea : A single linked list deletes a node with a specific value , Not used pre The pointer
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val==val)
head=head.next;
ListNode curr=head;
while(curr!=null&&curr.next!=null){
if(curr.next.val==val){
curr.next=curr.next.next;
}
curr=curr.next;
}
return head;
}
}
result :
With double pointer version code :
class Solution {
public ListNode deleteNode(ListNode head, int val) {
if(head.val == val)
return head.next;
ListNode pre = head, cur = head.next;
while(cur != null && cur.val != val) {
pre = cur;
cur = cur.next;
}
// Delete the last one cur node
if(cur != null)
pre.next = cur.next;
return head;
}
}
22.
subject :
idea : Reverse single chain table , Then count from front to back k Turn back and connect to head Back .
Code :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
// If :head=[1,2,3,4,5]
// Only parametric structures , So let the header node's val=-1
ListNode reverse=new ListNode(-1);
while(head!=null){
ListNode tmp=head.next;
head.next=reverse.next;
reverse.next=head;
head=tmp;
}
//reverse=[-1,5,4,3,2,1]
// here head=null
reverse=reverse.next;
// Only parametric structures , So let the header node's val=-1
head=new ListNode(-1);
while(reverse!=null){
ListNode tmp=reverse.next;
if(--k>=0){
reverse.next=head.next;
head.next=reverse;
}
reverse=tmp;
}
//head=[-1,4,5]
return head.next;
}
}
result :
25.
subject :
idea : Use a new header node res To save the linked list of results , Insert... Backward in turn l1/l2 The nodes in the ( This can only be done by increasing ),
Be careful : preservation res The head node of , Use later res Pointer traversal , The header node will be lost .
Code :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode res=new ListNode(-1);
ListNode resHead=res;
while(l1!=null&&l2!=null){
if(l1.val<l2.val){// Insert l1
res.next=l1;
l1=l1.next;
res=res.next;
}else if(l1.val>l2.val){// Insert l2
res.next=l2;
l2=l2.next;
res=res.next;
}else{
res.next=l1;
l1=l1.next;
res=res.next;
res.next=l2;
l2=l2.next;
res=res.next;
}
}
if(l1!=null){//l1 There's still... Left , Receive res Back
res.next=l1;
}else{//l2 There's still... Left , Receive res Back
res.next=l2;
}
return resHead.next;
}
}
result :
52.
subject :
doubt :
1. I don't understand why there are so many entries in this question , The results are not all given out , What else . The actual input parameters of the function only have two linked lists .
2.[4,1,8,4,5] and [5,0,1,8,4,5] The first intersection node of the two linked lists is 8. No 1. But this function alone should return 1 ah ? -> It should be otherwise stated in the main function .
Idea one : Double pointer ( Refer to the official explanation )
Code :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
// It's a little troublesome to write by yourself
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA==null||headB==null)
return null;
ListNode tmpA=headA;
ListNode tmpB=headB;
while(tmpA!=null || tmpB!=null){
if(tmpA==null){
tmpA=headB;
}else if(tmpB==null){
tmpB=headA;
}else{
if(tmpA==tmpB){
return tmpA;
}
tmpA=tmpA.next;
tmpB=tmpB.next;
}
}
// Reach the end at the same time
return null;
}
}
// Refer to the official explanation
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode tmpA = headA, tmpB = headB;
while (tmpA != tmpB) {
tmpA = tmpA == null ? headB : tmpA.next;
tmpB = tmpB == null ? headA : tmpB.next;
}
return tmpA;
}
}
result :
Official explanation There is another way to do this with a hash set , To learn :
Code :
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
Set<ListNode> visited = new HashSet<ListNode>();
ListNode temp = headA;
while (temp != null) {
visited.add(temp);
temp = temp.next;
}
temp = headB;
while (temp != null) {
if (visited.contains(temp)) {
return temp;
}
temp = temp.next;
}
return null;
}
}
result :
It's really slow
边栏推荐
- Niuke challenge 48 e speed instant forwarding (tree over tree)
- Difference between classification and regression
- How to check if a text field is empty or not in swift
- Educational Codeforces Round 117 (Rated for Div. 2)E. Messages
- CVPR 2022文档图像分析与识别相关论文26篇汇集简介
- [wc2006] director of water management
- 基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
- 9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
- Memory considerations under bug memory management
- First k large XOR value problem
猜你喜欢
Freefilesync folder comparison and synchronization software
Wechat applet -picker component is repackaged and the disabled attribute is added -- below
Wechat applet Registration Guide
Wechat applet -picker component is repackaged and the disabled attribute is added -- above
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
Installation and uninstallation of MySQL software for windows
33、使用RGBD相机进行目标检测和深度信息输出
程序员必备,一款让你提高工作效率N倍的神器uTools
Stream常用操作以及原理探索
随机推荐
【HCSD应用开发实训营】一行代码秒上云评测文章—实验过程心得
C language | file operation and error prone points
D check type is pointer
"Scoi2016" delicious problem solution
免费的机器学习数据集网站(6300+数据集)
Tips for using nexys A7 development board resources
Mongodb series window environment deployment configuration
Basic type of typescript
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
Included angle of 3D vector
7.Consul服务注册与发现
On insect classes and objects
Hard (magnetic) disk (I)
Gartner 2022年顶级战略技术趋势报告
GC is not used in D
9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Pointer
Svn commit error after deleting files locally