当前位置:网站首页>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 :

边栏推荐
- Codeforces Global Round 21A~D
- Teacher Li Hang's new book "machine learning methods" is on the market! Purchase link attached
- VTK 圆柱体的生成与渲染
- Niuke challenge 48 e speed instant forwarding (tree over tree)
- Zero basics of C language lesson 8: Functions
- Comparison of disk partition modes (MBR and GPT)
- Variable declaration of typescript
- [wc2006] director of water management
- 9項規定6個嚴禁!教育部、應急管理部聯合印發《校外培訓機構消防安全管理九項規定》
- 2021-10-18 character array
猜你喜欢

Never use redis expired monitoring to implement scheduled tasks!

Included angle of 3D vector

Win10 home vs pro vs enterprise vs enterprise LTSC

New specification of risc-v chip architecture

Codeforces Global Round 21A~D

Nexys A7开发板资源使用技巧

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

9项规定6个严禁!教育部、应急管理部联合印发《校外培训机构消防安全管理九项规定》

程序员必备,一款让你提高工作效率N倍的神器uTools
![[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
随机推荐
Traverse the specified directory to obtain the file name with the specified suffix (such as txt and INI) under the current directory
d检查类型是指针
DOS command
C language ---getchar() and putchar()
Memory considerations under bug memory management
In insect classes and objects
CF676C Vasya and String
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
Gartner 2022 Top Strategic Technology Trends Report
Use performance to see what the browser is doing
Logical operation
Wechat applet - bind and prevent event bubble catch
Is expression of D
Range of types
虫子 类和对象 上
Mongodb series window environment deployment configuration
Matlab programming related knowledge
程序员必备,一款让你提高工作效率N倍的神器uTools
Es common grammar I
Applicable and inapplicable scenarios of mongodb series
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/