LeetCode - 92. Reverse Linked List II
2016-07-03 18:58
381 查看
这道题目的要求是reverse Linked List中间的某一段,所以自然而然地想到将Linked List看作三部分,第一部分和第三部分不动,只是反转第二部分。另外,由于Linked List的头节点可能发生变化,所以要用到dummyNode这一技巧。Rerverse Linked List这一方法在前面的题目中已经看到过,这里不再重复,其实这道题目并没有很难的地方,只是各种指针的来回比较麻烦,也容易出错,所以这种题目要更加加以重视,它考察了一些知识点和技巧的综合,而且又有一些复杂,要多看几遍。时间复杂度O(n),代码如下:
/**
* 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 head;
// Set dummyNode
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
head = dummyNode;
ListNode nodeBeforeM;
ListNode nodeAtM;
ListNode nodeAtN;
ListNode nodeAfterN;
// Find ListNode at position m - 1 and m
for(int i = 0; i < m - 1; i++){
if(head == null) return null;
head = head.next;
}
nodeBeforeM = head;
nodeAtM = head.next;
nodeAtN = nodeAtM;
nodeAfterN = nodeAtM.next;
// Reverse Linked List from m to n
for(int i = m; i < n; i++){
if(nodeAfterN == null) return null;
ListNode next = nodeAfterN.next;
nodeAfterN.next = nodeAtN;
nodeAtN = nodeAfterN;
nodeAfterN = next;
}
// Set poitners at both sides
nodeAtM.next = nodeAfterN;
nodeBeforeM.next = nodeAtN;
return dummyNode.next;
}
}
知识点:
1. 在Linked List和Tree结构中,如果头节点或者root可能会发生改变,需要使用dummyNode
/**
* 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 head;
// Set dummyNode
ListNode dummyNode = new ListNode(0);
dummyNode.next = head;
head = dummyNode;
ListNode nodeBeforeM;
ListNode nodeAtM;
ListNode nodeAtN;
ListNode nodeAfterN;
// Find ListNode at position m - 1 and m
for(int i = 0; i < m - 1; i++){
if(head == null) return null;
head = head.next;
}
nodeBeforeM = head;
nodeAtM = head.next;
nodeAtN = nodeAtM;
nodeAfterN = nodeAtM.next;
// Reverse Linked List from m to n
for(int i = m; i < n; i++){
if(nodeAfterN == null) return null;
ListNode next = nodeAfterN.next;
nodeAfterN.next = nodeAtN;
nodeAtN = nodeAfterN;
nodeAfterN = next;
}
// Set poitners at both sides
nodeAtM.next = nodeAfterN;
nodeBeforeM.next = nodeAtN;
return dummyNode.next;
}
}
知识点:
1. 在Linked List和Tree结构中,如果头节点或者root可能会发生改变,需要使用dummyNode
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解