当前位置:网站首页>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
边栏推荐
- C language ---getchar() and putchar()
- GEE——全球人类居住区网格数据 1975-1990-2000-2014
- 虫子 STL string 下 练习题
- MySQL | basic commands
- Codeforces Global Round 21A~D
- Introduction to granular computing
- Bucket of P (segment tree + linear basis)
- Half search, character array definition, character array uses D11
- 8.Ribbon负载均衡服务调用
- Linear basis count (k large XOR sum)
猜你喜欢

9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions

Zero basics of C language lesson 7: break & continue

永远不要使用Redis过期监听实现定时任务!

9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》

嵌入式virlog代码运行流程

What is the use of index aliases in ES

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

Create your own cross domain proxy server

Basic type of typescript

程序员必备,一款让你提高工作效率N倍的神器uTools
随机推荐
It is better and safer to choose which securities company to open an account for flush stock
Cloudcompare - Poisson reconstruction
On insect classes and objects
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
[sdoi2013] forest
Range of types
Difference between classification and regression
Installation and uninstallation of MySQL software for windows
In insect classes and objects
9 regulations and 6 prohibitions! The Ministry of education and the emergency management department jointly issued the nine provisions on fire safety management of off campus training institutions
windows版MySQL软件的安装与卸载
Svn commit error after deleting files locally
[path of system analyst] Chapter 15 double disk database system (database case analysis)
Go language - pipeline channel
Common operation and Principle Exploration of stream
Zero basics of C language lesson 7: break & continue
Hard (magnetic) disk (II)
Formal parameters vs actual parameters
hands-on-data-analysis 第三单元 模型搭建和评估
Insect operator overloaded a fun
https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/
