当前位置:网站首页>Leetcode92. reverse linked list II
Leetcode92. reverse linked list II
2022-07-05 03:07:00 【what's your name.】
【 secondary 】 Give you the head pointer of the single linked list head And two integers left and right , among left <= right . Please reverse from position left To the position right The linked list node of , return Inverted list .
Example 1:
Input :head = [1,2,3,4,5], left = 2, right = 4
Output :[1,4,3,2,5]
Example 2:
Input :head = [5], left = 1, right = 1
Output :[5]
Tips :
The number of nodes in the linked list is n
1 <= n <= 500
-500 <= Node.val <= 500
1 <= left <= right <= n
Solution 1 :
ListNode m1(ListNode head, int left, int right) {
ListNode virtual = new ListNode(0, head);
int i = 1;
ListNode end = head;
ListNode prev = virtual;
while (i < right) {
if (i <= left-1) {
prev = prev.next;
}
end = end.next;
i++;
}
ListNode last_t = prev;
ListNode new_t = prev.next;
ListNode next_h = end.next;
last_t.next = reverse(prev.next, end);
new_t.next = next_h;
return virtual.next;
}
Solution 2 :
ListNode m2(ListNode head, int left, int right) {
ListNode v = new ListNode(0, head);
ListNode curr = head;
ListNode next = head.next;
ListNode x = v;
ListNode s = head;
ListNode e = head;
ListNode y = head.next;
for (int i = 1; i < right; i++) {
if (i == left-1) {
x = curr;
s = next;
}
if (i == right-1) {
e = next;
y = next.next;
}
ListNode next_h = next.next;
if (i >= left && i <= right-1) {
next.next = curr;
}
curr = next;
next = next_h;
}
x.next = e;
s.next = y;
return v.next;
}
ListNode reverse(ListNode start, ListNode end) {
ListNode prev = null;
ListNode curr = start;
ListNode stop = end.next;
while (curr != stop) {
ListNode next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return end;
}
边栏推荐
- Spark SQL learning bullet 2
- LeetCode 234. Palindrome linked list
- el-select,el-option下拉选择框
- Sqoop installation
- Asp+access campus network goods trading platform
- 问下,这个ADB mysql支持sqlserver吗?
- Basic authorization command for Curl
- Structure of ViewModel
- Share the newly released web application development framework based on blazor Technology
- Idea inheritance relationship
猜你喜欢
Voice chip wt2003h4 B008 single chip to realize the quick design of intelligent doorbell scheme
Utilisation simple de devtools
Accuracy problem and solution of BigDecimal
Sqoop installation
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
Share the newly released web application development framework based on blazor Technology
看 TDengine 社区英雄线上发布会,听 TD Hero 聊开发者传奇故事
Apache build web host
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)
随机推荐
【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)
Daily question 2 12
2021 Li Hongyi machine learning (3): what if neural network training fails
Returns the lowest common ancestor of two nodes in a binary tree
el-select,el-option下拉选择框
SPI and IIC communication protocol
[Yu Yue education] National Open University autumn 2018 8109-22t (1) monetary and banking reference questions
Usage scenarios and solutions of ledger sharing
There is a question about whether the parallelism can be set for Flink SQL CDC. If the parallelism is greater than 1, will there be a sequence problem?
PHP cli getting input from user and then dumping into variable possible?
Acwing第 58 场周赛【完结】
How to make OS X read bash_ Profile instead of Profile file - how to make OS X to read bash_ profile not . profile file
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
数据库和充值都没有了
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
When the low alcohol race track enters the reshuffle period, how can the new brand break the three major problems?
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
Problem solving: attributeerror: 'nonetype' object has no attribute 'append‘
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
Jd.com 2: how to prevent oversold in the deduction process of commodity inventory?