LeetCode-206. Reverse Linked List simply inverted list
Reverse a singly linked list.
Example:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
Follow up:
A linked list can be reversed either iteratively or recursively. Could you implement both?
Iterative method
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode cur = head;
ListNode pre = null;
while (cur != null) {
ListNode next = cur.next;
cur.next = pre;
pre = cur;
cur = next;
}
return pre;
}
}
Recursion
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode last = reverseList(head.next);
head.next.next = head;
head.next = null;
return last;
}
}
LeetCode-92. Reverse Linked List II reverse list from n to m
Iterative Method 1
For the list of issues, they are generally based on past experience to build a dummy node, the first node of the list even Uehara, so even if the head node changes, we can also be obtained by first forming new list of dummy-> next point. This question only requirement is that once traversed by the completion, took the title of example, the conversion of 2,3,4 three points, we need to find a first began to transform the former node node, so long as the pre-step to go back to m-1, minus 1 to why, because the title is counted from 1, only go one step here, is a node, point by pre it. How to do in case of node 1 is the beginning of conversion, which is why we use a dummy node, pre can also point to dummy node. Then it would start swapping, because you can only swap the two nodes, so we exchange the following order:
1 -> 2 -> 3 -> 4 -> 5 -> NULL
1 -> 3 -> 2 -> 4 -> 5 -> NULL
1 -> 4 -> 3 -> 2 -> 5 -> NULL
We can see, for a total nm step can be, is the first step back into the node 3 node 1, the second step will bear back into the 4-point node 1. This operation is very regular, then we say that a on the line, such as beginning, pre pointing to node 1, cur point node 2, and then we set up a temporary node t, pointing to the node 3 (note that we use temporary variable holds a node is to first disconnect the link between the node and the previous node, which can be used as a rule down), then we disconnect the node 2 and the node 3, the node 2 the next 4 connected to the junction, i.e. cur-> next = t-> next, and then the node 3 is connected to the junction point behind the front of the node 1 (i.e., node 2), i.e., t-> next = pre-> next, then the final disconnect of the original node 1 and the node 2, the node 1 to the node 3, i.e. pre-> next = t. Thus we have completed the node 3 will be removed, the rear node 1 is added. The second step will remove the node 4, node 1 is added to the rear, is the same operation, there is not much to say, please try it yourself under the bar, see the following code:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head;
for (int i = 1; i < m; i++) {
pre = pre.next;
cur = cur.next;
}
for (int i = 0; i < n - m; i++) {
ListNode next = cur.next;
cur.next = next.next;
next.next = pre.next;
pre.next = next;
}
return dummy.next;
}
}
Iterative Method 2
Basically the same idea, put to the back of the interval to be reversed
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if (head == null || head.next == null) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
ListNode mNode = head;
ListNode nNode = head;
for (int i = 1; i < m; i++) {
pre = pre.next;
mNode = mNode.next;
}
for (int i = 1; i < n; i++) {
nNode = nNode.next;
}
while (mNode != nNode) {
pre.next = mNode.next;
mNode.next = nNode.next;
nNode.next = mNode;
mNode = pre.next;
}
return dummy.next;
}
}
Recursion
ListNode next = null;
ListNode reverseN(ListNode head, int n) {
if (n == 1) {
next = head.next;
return head;
}
ListNode last = reverseN(head.next, n - 1);
head.next.next = head;
head.next = next;
return last;
}
ListNdoe reverseBetween(ListNode head, int m, int n) {
if (m == 1) {
return reverseN(head, n);
}
head.next = reverseBetween(head.next, m - 1, n - 1);
return head;
}
LeetCode-24. Swap Nodes in Pairs
Iterative method
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) {
return head;
}
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
while (pre.next != null && pre.next.next != null) {
ListNode temp = pre.next.next;
pre.next.next = temp.next;
temp.next = pre.next;
pre.next = temp;
pre = temp.next;
}
return dummy.next;
}
}
Recursive wording
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
if (head == null || head.next == null) return head;
ListNode temp = head.next;
head.next = swapPairs(head.next.next);
temp.next = head;
return temp;
}
}
Leetcode-25. Reverse Nodes in k-Group
Wording of Iteration 1: stack-based secondary storage
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) {
return head;
}
Stack<ListNode> stack = new Stack<>();
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode cur = dummy;
ListNode next = dummy.next;
while (next != null) {
for (int i = 0; i < k && next != null; i++) {
stack.push(next);
next = next.next;
}
if (stack.size() != k) return dummy.next;
while (stack.size() != 0) {
cur.next = stack.pop();
cur = cur.next;
}
cur.next = next;
}
return dummy.next;
}
}
The wording of Iteration 2: must first count the length of the list
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || head.next == null) return head;
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode pre = dummy;
ListNode cur = head;
int len = 0;
while (cur != null) {
cur = cur.next;
len++;
}
cur = dummy.next;
for (int i = 0; i < len / k; i++) {
for (int j = 0; j < k - 1; j++) {
ListNode temp = cur.next;
cur.next = temp.next;
temp.next = pre.next;
pre.next = temp;
}
pre = cur;
cur = pre.next;
}
return dummy.next;
}
}