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

边栏推荐
猜你喜欢

Hard (magnetic) disk (II)

Relevant knowledge of information entropy

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

Installation and uninstallation of MySQL software for windows

Gartner 2022 Top Strategic Technology Trends Report

Gartner 2022年顶级战略技术趋势报告

微信小程序注册指引

7.consul service registration and discovery

Self created notes (unique in the whole network, continuously updated)

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
随机推荐
ThreadLocal giant pit! Memory leaks are just Pediatrics
Pointer
FreeFileSync 文件夹比较与同步软件
First k large XOR value problem
虫子 类和对象 中
Record: why is there no lightning 4 interface graphics card docking station and mobile hard disk?
33、使用RGBD相机进行目标检测和深度信息输出
Basic type of typescript
ThreadLocal巨坑!内存泄露只是小儿科...
PHP非对称加密算法(RSA)加密机制设计
[jsoi2015] string tree
虫子 类和对象 上
[MySQL from introduction to mastery] [advanced part] (II) representation of MySQL directory structure and tables in the file system
CF676C Vasya and String
DOS command
Pycharm远程连接服务器来跑代码
The most critical elements of team management
MySQL | basic commands
C language ---getchar() and putchar()
Obtain information about hard disk and volume or partition (capacity, ID, volume label name, etc.)
https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/