当前位置:网站首页>Enter the starting position, the ending position intercepts the linked list
Enter the starting position, the ending position intercepts the linked list
2022-08-11 03:33:00 【这是一本书】
1 Background
Enter m n , linked list, and get a linked list with starting point m and ending point n
2 Problem-solving ideas
1) First, get N nodes according to the input n, see this article for details
https://blog.csdn.net/an13654067079/article/details/126173743
2) m node, n node m node as the head of the new linked list
3) The previous node of the m node mPre.next = null
The next node of n nodes nNext.next = null
The new linked list is intercepted, the problem is not difficult
public static void main(String[] args) {ListNode listNode = ListNodeUtil.getListNode();int m = 2;int n = 4;//Get m nodes, n nodesListNode mNode = index(listNode, m);ListNode nNode = index(listNode, n);if(m!=1){//Get the previous node of mint mPre = m-1;ListNode mPreNode = index(listNode, mPre);mPreNode.next = null;}ListNode nNextNode = nNode.next;nNode.next = null;System.out.println(mNode);}/*** Get the node at the specified location** @param listNode* @param index* @return*/public static ListNode index(ListNode listNode , int index){ListNode dummyNode = new ListNode();dummyNode.setVal(0);dummyNode.setNext(listNode);int n = 1;for(int i = 0 ; i< index; i++){dummyNode = dummyNode.getNext();}return dummyNode;}
import lombok.Data;@Datapublic class ListNode {/*** current node value*/public int val;/*** next node*/public ListNode next ;}
public class ListNodeUtil {/*** Get the linked list** @return*/public static ListNode getListNode(){ListNode one = new ListNode();one.setVal(1);ListNode two = new ListNode();two.setVal(2);ListNode three = new ListNode();three.setVal(3);ListNode four = new ListNode();four.setVal(4);ListNode five = new ListNode();five.setVal(5);one.setNext(two);two.setNext(three);three.setNext(four);four.setNext(five);return one;}}
边栏推荐
- 多串口RS485工业网关BL110
- E-commerce project - mall time-limited seckill function system
- What is third-party payment?
- Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
- A brief analysis of whether programmatic futures trading or manual order is better?
- 7 sorting algorithms that are often tested in interviews
- 作业8.10 TFTP协议 下载功能
- How to delete statements audit log?
- 用户如何克服程序化交易中的情绪问题?
- 二叉树相关代码题【较全】C语言
猜你喜欢
电力机柜数据监测RTU
Briefly, talk about the use of @Transactional in the project
Detailed explanation of VIT source code
Redis老了吗?Redis与Dragonfly性能比较
【FPGA】day18-ds18b20实现温度采集
A simple JVM tuning, learn to write it on your resume
leetcode: 358. Reorder strings at K distance intervals
[BX] and loop
rac备库双节点查询到的表最后更新时间不一致
Salesforce disbands the Chinese team, which CRM product is more suitable for the Chinese
随机推荐
电力机柜数据监测RTU
JS-DOM element object
【FPGA】day18-ds18b20实现温度采集
Is Redis old?Performance comparison between Redis and Dragonfly
How can users overcome emotional issues in programmatic trading?
互换性与测量技术——表面粗糙度选取和标注方法
互换性与测量技术-公差原则与选用方法
Leetcode 669. 修剪二叉搜索树
C语言之自定义类型------结构体
Differences and connections between distributed and clustered
【LeetCode】Day112-重复的DNA序列
[DB operation management/development solution] Shanghai Daoning provides you with an integrated development tool to improve the convenience of work - Orange
The development of the massage chair control panel makes the massage chair simple and intelligent
多商户商城系统功能拆解26讲-平台端分销设置
2022-08-10 第六小组 瞒春 学习笔记
Will oracle cardinality affect query speed?
程序化交易改变了什么?
【愚公系列】2022年08月 Go教学课程 036-类型断言
Window function application of sum and count
【FPGA】day20-I2C读写EEPROM