GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/ReversedLinkedListII.java at master · LeBW/leetcode

Expand file tree

/

ReversedLinkedListII.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
40 lines (39 loc) · 1.14 KB
/

ReversedLinkedListII.java

Copy path

File metadata and controls

40 lines (39 loc) · 1.14 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 92. Reversed LinkedList II
* 链表翻转
* @author LBW
*/
public class ReversedLinkedListII {
public ListNode reverseBetween(ListNode head, int left, int right) {
// 使用虚拟头节点避免分类讨论
ListNode dummy = new ListNode(-1);
dummy.next = head;
// 找到 left 前的节点和 right 后的节点
ListNode leftNode = dummy;
for (int i = 0; i < left - 1; i++) {
leftNode = leftNode.next;
}
ListNode cur = leftNode;
for (int i = 0; i < right - left + 1; i++) {
cur = cur.next;
}
// 切断,进行翻转
ListNode rightNode = cur.next;
cur.next = null;
ListNode reverseHead = reverse(leftNode.next);
// 连接
leftNode.next.next = rightNode;
leftNode.next = reverseHead;
return dummy.next;
}
private ListNode reverse(ListNode head) {
ListNode pre = null, cur = head, next = head.next;
while (cur != null) {
next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.