当前位置:网站首页>Linked list (simple)
Linked list (simple)
2022-07-05 13:50:00 【Anna and her notebook】
The finger of the sword Offer 06. Print linked list from end to end
The finger of the sword Offer 24. Reverse a linked list
The finger of the sword Offer 35. Replication of complex linked list
The finger of the sword Offer 06. Print linked list from end to end - Power button (LeetCode)
Definition of a single linked list :
/*Definition for singly-linked list:*/
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
One : Stack method
class Solution {
public int[] reversePrint(ListNode head) {
Stack<ListNode> stack=new Stack<>();
ListNode cur=head;
while(cur!=null){
stack.push(cur);
cur=cur.next;
}
int[] arr=new int[stack.size()];
for(int i=0;i<arr.length;i++){
arr[i]=stack.pop().val;
}
return arr;
}
}
/** Complexity analysis : Time complexity O(N) Spatial complexity O(N) */
class Solution {
public int[] reversePrint(ListNode head) {
LinkedList<Integer> stack = new LinkedList<Integer>();
while(head != null) {
stack.addLast(head.val);//addLast() Method is used in LinkedList Insert a specific element at the end of
head = head.next;
}
int[] res = new int[stack.size()];
for(int i = 0; i < res.length; i++)
res[i] = stack.removeLast();//removeLast() Method is used to get from LinkedList Delete the last element in
return res;
}
}
/** Complexity analysis : Time complexity O(N): Stack in and stack out are used together O(N) Time . Spatial complexity O(N): Auxiliary stack stack And an array res Use together O(N) Extra space */
Two : Recursive method
Algorithm flow :
- Recurrence stage : Every time I pass in head.next , With head == null( That is, walk through the tail node of the linked list ) Is a recursive termination condition , Go straight back to .
- Backtracking phase : When you go back layer by layer , Add the current node value to the list , namely tmp.add(head.val).
Final , Will list tmp Convert to array res , And return .
Complexity analysis
- Time complexity O(N): Traversing the linked list , recursive N Time .
- Spatial complexity O(N): System recursion needs to use O(N) Stack space of .
class Solution {
ArrayList<Integer> tmp = new ArrayList<Integer>();
public int[] reversePrint(ListNode head) {
recur(head);
int[] res = new int[tmp.size()];
for(int i = 0; i < res.length; i++)
res[i] = tmp.get(i);
return res;
}
void recur(ListNode head) {
if(head == null) return;
recur(head.next);
tmp.add(head.val);
}
}
3、 ... and :
class Solution {
// Execution time : 0 ms, In all Java Defeated in submission 100.00% Users of
// Memory consumption : 39.8 MB, In all Java Defeated in submission 100.00% Users of
// No stack , Do not use recursion , Anyway, it is also scanned twice , Why allocate extra space ?
public static int[] reversePrint(ListNode head) {
ListNode node = head;
int count = 0;
while (node != null) {
++count;
node = node.next;
}
int[] nums = new int[count];
node = head;
for (int i = count - 1; i >= 0; --i) {
nums[i] = node.val;
node = node.next;
}
return nums;
}
}
The finger of the sword Offer 24. Reverse a linked list - Power button (LeetCode)
One : iteration
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public ListNode reverseList(ListNode head) {
if(head==null || head.next==null) return head;
ListNode pre=null;
ListNode cur=head;
ListNode tmp=null;
while(cur!=null){
tmp=cur.next;// Staging successor nodes cur.next
cur.next=pre;// modify next Reference point
pre=cur;// pre The staging cur
cur=tmp;// cur Visit the next node
}
return pre;
}
}
Two : recursive
class Solution {
public ListNode reverseList(ListNode head) {
return recur(head, null); // Call recursion and return
}
private ListNode recur(ListNode cur, ListNode pre) {
if (cur == null){
return pre; // Termination conditions
}
ListNode res = recur(cur.next, cur); // Recursive successor node
cur.next = pre; // Modify the node reference to
return res; // Returns the header node of the inverted linked list
}
}
The finger of the sword Offer 35. Replication of complex linked list - Power button (LeetCode)
One : Hashtable
/* // 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) {
Map<Node, Node> m = new HashMap<>();
Node p = head;
while(head != null) {
Node t = new Node(head.val);
m.put(head, t);
head = head.next;
}
head = p;
// According to the original linked list , Look up the hash table and connect the new linked list
while(head != null) {
Node n = head.next; // The next node of the original node
Node r = head.random;// Original node random node
// New nodes next Point to the next new node
m.get(head).next = m.get(n);
// Set the new node's random The node points to
m.get(head).random = m.get(r);
head = head.next;
}
return m.get(p);
}
}
class Solution {
public Node copyRandomList(Node head) {
if(head == null) return null;
Node cur = head;
Map<Node, Node> map = new HashMap<>();
// Copy each node , And establish “ Original node -> New node ” Of Map mapping
while(cur != null) {
map.put(cur, new Node(cur.val));
cur = cur.next;
}
cur = head;
// To build a new linked list next and random Point to
while(cur != null) {
map.get(cur).next = map.get(cur.next);
map.get(cur).random = map.get(cur.random);
cur = cur.next;
}
// Return the head node of the new linked list
return map.get(head);
}
}
边栏推荐
- Rk3566 add LED
- js 从一个数组对象中取key 和value组成一个新的对象
- asp. Net read TXT file
- Matlab paper chart standard format output (dry goods)
- [MySQL usage Script] catch all MySQL time and date types and related operation functions (3)
- asp.net 读取txt文件
- 蓝桥杯学习2022.7.5(上午)
- Data Lake (VII): Iceberg concept and review what is a data Lake
- Idea设置方法注释和类注释
- jasypt配置文件加密|快速入门|实战
猜你喜欢

Summit review | baowanda - an integrated data security protection system driven by compliance and security

Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care

Zibll theme external chain redirection go page beautification tutorial

龙芯派2代烧写PMON和重装系统

华为推送服务内容,阅读笔记

Embedded software architecture design - message interaction

Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"

Solve the problem of invalid uni app configuration page and tabbar

Redis6 master-slave replication and clustering

:: ffff:192.168.31.101 what address is it?
随机推荐
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
The development of speech recognition app with uni app is simple and fast.
Redis6 data type and operation summary
Pancake Bulldog robot V2 (code optimized)
基于微信小程序的订餐系统
2022司钻(钻井)考试题库及模拟考试
我为什么支持 BAT 拆掉「AI 研究院」
Huawei push service content, read notes
redis6数据类型及操作总结
Prefix, infix, suffix expression "recommended collection"
STM32 reverse entry
华为推送服务内容,阅读笔记
Embedded software architecture design - message interaction
laravel-dompdf导出pdf,中文乱码问题解决
Godson 2nd generation burn PMON and reload system
[public class preview]: basis and practice of video quality evaluation
Jasypt configuration file encryption | quick start | actual combat
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
Scientific running robot pancakeswap clip robot latest detailed tutorial
Zhubo Huangyu: it's really bad not to understand these gold frying skills