详细见:leetcode.com/problems/reverse-linked-list-ii
Java Solution: github
package leetcode;
/*
* Reverse a linked list from position m to n. Do it in-place and in one-pass.
For example:
Given 1->2->3->4->5->NULL, m = 2 and n = 4,
return 1->4->3->2->5->NULL.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.
*/
import tools.ListNode辅助.ListNode;;
public class P092_ReverseLinkedListII {
public static void main(String[] args) {
ListNode head = tools.ListNode辅助.A_一维生成器(new int[] {});
ListNode ans = new Solution().reverseBetween(head, 1, 1);
tools.ListNode辅助.B_打印链表(ans);
}
/*
* 题目非常容易,一次AC,好爽
* 1 ms
*/
static class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (m >= n) {
return head;
}
ListNode tra = head, one_last = null, two_first = null,
two_last = null, three_first = null, save_tra_next = null;
int count = 1;
while (tra != null) {
save_tra_next = tra.next;
if (count == m - 1) {
one_last = tra;
} else if (count == m) {
two_first = tra;
two_last = tra;
} else if (count == n + 1) {
three_first = tra;
} else if (count > m && count <= n) {
tra.next = two_first;
two_first = tra;
}
tra = save_tra_next;
count ++;
}
if (one_last != null) {
one_last.next = two_first;
}
if (two_last != null) {
two_last.next = three_first;
}
if (one_last == null) {
return two_first;
}
return head;
}
}
}
C Solution: github
/*
url: leetcode.com/problems/reverse-linked-list-ii
AC 0ms 73.39%
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode sln;
typedef struct ListNode * pln;
struct ListNode {
int val;
struct ListNode * next;
};
pln convert_int_to_ListNode(int * arr, int n) {
pln head = NULL;
pln travel = NULL;
pln temp = NULL;
int i = 0;
if (n == 0 || n < 0) return NULL;
travel = (pln) malloc(sizeof(sln));
travel->val = *(arr + 0);
travel->next = NULL;
head = travel;
for (i = 1; i < n; i ++) {
temp = (pln) malloc(sizeof(sln));
temp->val = *(arr + i);
temp->next = NULL;
travel->next = temp;
travel = travel->next;
}
return head;
}
void free_ListNode(struct ListNode * l) {
struct ListNode * l1 = l, * l2 = l;
if (l == NULL) return;
while (l1 != NULL) {
l2 = l2->next;
free(l1);
l1 = l2;
}
}
void print_ListNode(pln l) {
while (l != NULL) {
printf("%d ", l->val);
l = l->next;
}
printf("\r\n");
}
pln reverseBetween(pln h, int m, int n) {
int i = 1;
pln t = h, s = NULL, p = NULL;
pln h1 = NULL, h2 = NULL;
pln e1 = NULL, e2 = NULL;
while (t != NULL) {
s = t->next;
if (i == m-1) e1 = t;
if (i == n+1) {
e2 = t;
break;
}
if (i == m) h1 = t;
if (i == n) h2 = t;
if (i > m && i <= n) {
t->next = p;
}
p = t;
t = s;
i ++;
}
if (e1 != NULL) e1->next = h2;
if (h1 != NULL) h1->next = e2;
return m == 1 ? h2 : h;
}
int main() {
/*
1, 2, 3, 4, 5, 6, 7, 8, 9
1, 2
1, 1
2, 4
*/
int a[] = {1};
pln h = convert_int_to_ListNode(a, 1);
pln ans = reverseBetween(h, 1, 1);
print_ListNode(ans);
return 0;
}
Python Solution: github
#coding=utf-8
'''
url: leetcode.com/problems/reverse-linked-list-ii
@author: zxwtry
@email: zxwtry@qq.com
@date: 2017年4月23日
@details: Solution: 46ms 49.65%
'''
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
def __str__(self, *args, **kwargs):
return str(self.val)
def con(l):
n = [ListNode(v) for v in l]
for i in range(len(l)-1):
n[i].next = n[i+1]
return n[0]
def pri(d):
print("=====================")
while d != None:
print(d.val)
d = d.next
print("=====================")
class Solution(object):
def reverseBetween(self, h, m, n):
"""
:type h: ListNode
:type m: int
:type n: int
:rtype: ListNode
"""
t, d, p = 1, h, None
h1, h2, e1, e2 = None, None, None, None
while d != None:
if t == m-1: e1=d
if t == m: h1=d
if t == n: h2=d
if t == n+1:
e2=d
break
s = d.next
if t >= m:
d.next=p
d, t, p = s, t+1, d
if e1 != None: e1.next = h2
if h1 != None: h1.next = e2
if m == 1: return h2
return h
if __name__ == "__main__":
h = con([1, 2, 3, 4, 5])
a = Solution().reverseBetween(h, 1, 5)
pri(a)