Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit31d03a4

Browse files
author
applewjg
committed
Reverse Linked List II
Change-Id: I89ec151fb0967b458f61643623a5ba5de943d993
1 parent93a4f4d commit31d03a4

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

‎ReverseLinkedListII.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Jan 2, 2015
4+
Problem: Reverse Linked List II
5+
Difficulty: Easy
6+
Source: https://oj.leetcode.com/problems/reverse-linked-list-ii/
7+
Notes:
8+
Reverse a linked list from position m to n. Do it in-place and in one-pass.
9+
For example:
10+
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
11+
return 1->4->3->2->5->NULL.
12+
Note:
13+
Given m, n satisfy the following condition:
14+
1 <= m <= n <= length of list.
15+
16+
Solution: in-place & one-pass.
17+
*/
18+
19+
/**
20+
* Definition for singly-linked list.
21+
* public class ListNode {
22+
* int val;
23+
* ListNode next;
24+
* ListNode(int x) {
25+
* val = x;
26+
* next = null;
27+
* }
28+
* }
29+
*/
30+
publicclassSolution {
31+
publicListNodereverseBetween(ListNodehead,intm,intn) {
32+
ListNodedummy =newListNode(-1);
33+
dummy.next =head;
34+
ListNodefirst =dummy;
35+
for (inti =0;i <m -1; ++i)first =first.next;
36+
ListNodecur =first.next;
37+
for (inti =0;i <n -m; ++i) {
38+
ListNodemove =cur.next;
39+
cur.next =move.next;
40+
move.next =first.next;
41+
first.next =move;
42+
}
43+
returndummy.next;
44+
}
45+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp