当前位置:网站首页>Sword finger offer 06.24.35 Linked list
Sword finger offer 06.24.35 Linked list
2022-06-26 14:13:00 【hedgehog:】
Topic 1 :

idea : Put the linked list into the array in reverse order Then return the array
Code :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
ListNode node=head;
Stack<Integer> st= new Stack<>();
int cnt=0;
int i=0;
if(node==null)
return new int[0];
// Count the number of nodes
while(node.next!=null){
node=node.next;
cnt++;
}
node=head;
int[] print = new int[cnt+1];
// Put the linked list into the array in reverse order
for(i=cnt;i>=0;i--){
print[i]=node.val;
node=node.next;
}
return print;
}
}result :

Topic two :

idea : Create a new header node Reverse a linked list
Code :
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
ListNode node;
ListNode reverse = new ListNode();
reverse.next=head;
if(head==null)
return null;
while(head.next!=null){
node=head.next;
head.next=node.next;
node.next=reverse.next;
reverse.next=node;
}
return reverse.next;
}
}result :

Topic three :



idea : Repeat this node after each node And then the odd position of the node random Field to the next node Finally, split the original linked list and the result linked list
Code :
/*
// Definition for a Node.
class Node {
int val;
Node next;
Node random;
public Node(int val) {
this.val = val;
this.next = null;
this.random = null;
}
}
*/
class Solution {
public Node copyRandomList(Node head) {
if(head==null)
return null;
// Auxiliary nodes
Node curr = head;
// Repeat each node Form a new linked list
// Traverse each node So the termination conditions :curr!=null
while(curr!=null){
Node temp=new Node(curr.val);
temp.next=curr.next;
curr.next=temp;
curr=curr.next.next; // or curr=temp.next;
}
// Give duplicate nodes Fu random Domain value
curr=head;
// Through each ( Singular position ) node So the termination conditions :curr!=null
while(curr!=null){
// Empty is already empty without assignment
if(curr.random!=null)
curr.next.random=curr.random.next;
curr=curr.next.next;
}
// Split two linked lists of odd and even numbers
//ori For the original linked list because 【 Copy the linked list 】 It is required not to change the original linked list
Node ori=head;
//res Point to the new list ( Result list ) The head of the
Node res=head.next;
curr=head.next;
// Traverse to the last node and stop So the termination conditions :curr.next!=null
while(curr.next!=null){
ori.next=ori.next.next;
curr.next=curr.next.next;
ori=ori.next;
curr=curr.next;
}
// Finally, don't forget to empty the end of the original linked list
ori.next=null;
// Return result list
return res;
}
}result :

边栏推荐
- It is better and safer to choose which securities company to open an account for flush stock
- Installation and uninstallation of MySQL software for windows
- AGCO AI frontier promotion (6.26)
- On insect classes and objects
- Free machine learning dataset website (6300+ dataset)
- Difference between classification and regression
- 数学建模经验分享:国赛美赛对比/选题参考/常用技巧
- In insect classes and objects
- 9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
- 9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》
猜你喜欢

Postman自动化接口测试
![[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system](/img/03/a1885e4740bbfdbdee2446e3dd81d0.png)
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system

Build your own PE manually from winpe of ADK

服务器创建虚拟环境跑代码

Detailed sorting of HW blue team traceability process

程序员必备,一款让你提高工作效率N倍的神器uTools

Network remote access using raspberry pie

Zero basics of C language lesson 7: break & continue

8.Ribbon负载均衡服务调用

RISC-V 芯片架构新规范
随机推荐
微信小程序注册指引
Mathematical design D12 according to string function
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
ICML 2022 | LIMO: 一种快速生成靶向分子的新方法
Nexys A7开发板资源使用技巧
Wechat applet -picker component is repackaged and the disabled attribute is added -- above
Luogu p4513 xiaobaiguang Park
爱可可AI前沿推介(6.26)
Exercises under insect STL string
8. Ribbon load balancing service call
Applicable and inapplicable scenarios of mongodb series
Bucket of P (segment tree + linear basis)
Is expression of D
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
Select tag - uses the default text as a placeholder prompt but is not considered a valid value
I met the problem of concurrent programming in an interview: how to safely interrupt a running thread
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
C language | Consortium
FreeFileSync 文件夹比较与同步软件
Niuke challenge 48 e speed instant forwarding (tree over tree)
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/