当前位置:网站首页>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;
}
边栏推荐
- Azkaban overview
- [200 opencv routines] 99 Modified alpha mean filter
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 1)
- Hmi-32- [motion mode] add light panel and basic information column
- Devtools的簡單使用
- Flume配置4——自定义MYSQLSource
- 返回二叉树中两个节点的最低公共祖先
- Anchor free series network yolox source code line by line explanation Part 2 (a total of 10, ensure to explain line by line, after reading, you can change the network at will, not just as a participan
- Dart series: collection of best practices
- ELK日志分析系统
猜你喜欢

Utilisation simple de devtools

【LeetCode】106. Construct binary tree from middle order and post order traversal sequence (wrong question 2)

【LeetCode】98. Verify the binary search tree (2 brushes of wrong questions)

Design and implementation of community hospital information system

Machine learning experiment report 1 - linear model, decision tree, neural network part

Logstash、Fluentd、Fluent Bit、Vector? How to choose the appropriate open source log collector

Bumblebee: build, deliver, and run ebpf programs smoothly like silk

The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code

Use of kubesphere configuration set (configmap)

Pat grade a 1119 pre- and post order traversals (30 points)
随机推荐
有個疑問 flink sql cdc 的話可以設置並行度麼, 並行度大於1會有順序問題吧?
Pat class a 1160 forever (class B 1104 forever)
Anchor free series network yolox source code line by line explanation four (a total of ten, ensure line by line explanation, after reading, you can change the network at will, not just as a participan
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
Simple use of devtools
Pat class a 1162 postfix expression
Linux安装Redis
Asemi rectifier bridge 2w10 parameters, 2w10 specifications, 2w10 characteristics
Structure of ViewModel
Use UDP to send a JPEG image, and UPD will convert it into the mat format of OpenCV after receiving it
Azkaban安装部署
040. (2.9) relieved
The perfect car for successful people: BMW X7! Superior performance, excellent comfort and safety
SQL injection exercise -- sqli Labs
Design and practice of kubernetes cluster and application monitoring scheme
ASP. Net core 6 framework unveiling example demonstration [01]: initial programming experience
The latest blind box mall, which has been repaired very popular these days, has complete open source operation source code
Cette ADB MySQL prend - elle en charge SQL Server?
Utilisation simple de devtools
Sqoop installation