当前位置:网站首页>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
边栏推荐
- Gee - Global Human Settlements grid data 1975-1990-2000-2014
- 团队管理的最关键因素
- Linear basis
- d的is表达式
- 【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
- 8. Ribbon load balancing service call
- Assert and constd13
- Wechat applet - bind and prevent event bubble catch
- Luogu p4145 seven minutes of God created questions 2 / Huashen travels around the world
- [cqoi2015] task query system
猜你喜欢

ThreadLocal巨坑!内存泄露只是小儿科...

使用 Performance 看看浏览器在做什么

New specification of risc-v chip architecture

Global variable vs local variable

What is the use of index aliases in ES

Wechat applet -picker component is repackaged and the disabled attribute is added -- below

8. Ribbon load balancing service call

Build your own PE manually from winpe of ADK

33、使用RGBD相机进行目标检测和深度信息输出

Postman自动化接口测试
随机推荐
Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient
Bucket of P (segment tree + linear basis)
33. Use rgbd camera for target detection and depth information output
Design of PHP asymmetric encryption algorithm (RSA) encryption mechanism
Variable declaration of typescript
A solution to the problem that the display of newff function in neural network cannot be converted from double to struct
Notes: the 11th and 12th generation mobile versions of Intel support the native thunderbolt4 interface, but the desktop version does not
New specification of risc-v chip architecture
Free machine learning dataset website (6300+ dataset)
虫子 运算符重载的一个好玩的
C language | file operation and error prone points
33、使用RGBD相机进行目标检测和深度信息输出
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
[jsoi2015] string tree
Zero basics of C language lesson 8: Functions
FreeFileSync 文件夹比较与同步软件
Applicable and inapplicable scenarios of mongodb series
Relevant knowledge of information entropy
Tips for using nexys A7 development board resources
【Proteus仿真】Arduino UNO按键启停 + PWM 调速控制直流电机转速
https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
