My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || m <= 0 || n <= 0 || m > n)
return null;
else if (head.next == null || m == n)
return head;
int counter = 1;
ListNode dummy = new ListNode(Integer.MIN_VALUE);
dummy.next = head;
ListNode temp = dummy.next;
ListNode preM = null;
ListNode M = null;
ListNode N = null;
ListNode preTemp = dummy;
while (temp != null) {
if (counter == m) {
preM = preTemp;
M = temp;
break;
}
counter++;
temp = temp.next;
preTemp = preTemp.next;
}
temp = temp.next;
ListNode tempM = M;
for (int i = m + 1; i <= n; i++) {
if (i == n)
N = temp;
ListNode p = temp.next;
temp.next = tempM;
tempM = temp;
temp = p;
}
preM.next = N;
M.next = temp;
return dummy.next;
}
public static void main(String[] args) {
Solution test = new Solution();
ListNode n1 = new ListNode(1);
ListNode n2 = new ListNode(2);
ListNode n3 = new ListNode(3);
ListNode n4 = new ListNode(4);
ListNode n5 = new ListNode(5);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
System.out.println(test.reverseBetween(n1, 1, 4));
}
}
My test result:
这道题目一开始理解错题意了。。真是啃爹。
然后,以前反转链表,我都是递归做的。
没想到可以直接一遍反转,学习了。
dummy.next = head;
preTemp = head;
temp = head.next;
head.next = null;
while (temp != null) {
ListNode p = temp.next;
temp.next = preTemp;
preTemp = temp;
temp = p;
}
dummy.next = preTemp;
return dummy.next;
差不多就该长这个样子。
代码写的还是很丑。
下面是重点。
我发现,当我使用了如下模型,
pre -> curr -> post
之后,大多数链表中的删除结点,反转操作。我都可以很顺畅的写下来!
一定要记住这个办法。
代码如下:
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || m <= 0 || n <= 0 || m > n)
return null;
else if (head.next == null || m == n)
return head;
int count = 0;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = null;
ListNode curr = dummy;
while (curr.next != null) {
pre = curr;
curr = curr.next;
count++;
if (count == m)
break;
}
ListNode post = curr.next;
curr.next = null;
for (int i = m; i < n; i++) {
ListNode temp = post.next;
post.next = curr;
curr = post;
post = temp;
}
pre.next.next = post;
pre.next = curr;
return dummy.next;
}
}
**
总结: 反转链表,非递归,一遍过。
**
Anyway, Good luck, Richardo!
My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null)
return head;
/** assume m and n are under rules */
int counter = 0;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode curr = head;
ListNode pre = dummy;
while (curr != null) {
counter++;
if (counter == m)
break;
else {
pre = curr;
curr = curr.next;
}
}
ListNode post = curr.next;
while (post != null) {
counter++;
if (counter <= n) {
ListNode temp = post.next;
post.next = curr;
curr = post;
post = temp;
}
else
break;
}
pre.next.next = null;
if (post != null)
pre.next.next = post;
pre.next = curr;
return dummy.next;
}
}
采用了 dummy node + 反转链表。不难。有点小烦。
Anyway, Good luck, Richardo!
My code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || m > n) {
return null;
}
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode pre = dummy;
ListNode curr = dummy.next;
int counter = 1;
ListNode pre_m = null;
while (curr != null) {
if (counter == m) {
pre_m = pre;
break;
}
pre = curr;
curr = curr.next;
counter++;
}
ListNode curr_next = curr.next;
for (int i = 0; i < n - m; i++) {
ListNode next = curr_next.next;
curr_next.next = curr;
curr = curr_next;
curr_next = next;
}
ListNode pre_m_next = pre_m.next;
pre_m.next = curr;
pre_m_next.next = curr_next;
return dummy.next;
}
}
一开始理解错题意了,以为要 replace,其实是reverse部分链表。
然后有点麻烦吧。勉强多次提交做出来了。。
Anyway, Good luck, Richardo! --- 08/15/2016