LeetCode-206。リンクリストを逆に単に反転リスト
単独リンクリストを逆にします。
例:
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL
ファローアップ:
リンクリストは、いずれかの繰り返しや再帰的に反転させることができます。あなたは両方を実装してもらえますか?
反復法
/**
* 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;
}
}
再帰
/**
* 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。NからMへのリンクリストII逆リストリバース
反復法1
問題のリストについては、それらは一般的にも、ヘッドノードが変化した場合ので、我々はまた、dummy->次の最初の形成、新しいリストによって得ることができ、ダミーノード、リストの最初のノードでも上原を構築するために、過去の経験に基づいていますポイント。この質問唯一の要件が完了によって横断たら、例のタイトルを取ったことで、2,3,4 3点の変換は、我々が最初に元のノードのノードを変換するために始めた見つける必要があります、タイトルは1からカウントされるので、それほど長いバックM-1に行くために前工程として、マイナス1理由に、ここでしか一歩を行く、事前にそれによってノード、ポイントです。ノード1の場合に行うにはどのように我々は、ダミーノードを使用する理由である変換、の始まりで、前にもダミーノードを指すことができます。そして、それは我々が次の順序を交換するので、あなたは、2つのノードだけ入れ替えることができますので、スワッピングを開始します:
1 - > 2 - > 3 - > 4 - > 5 - > NULL
1 - > 3 - > 2 - > 4 - > 5 - > NULL
1 - > 4 - > 3 - > 2 - > 5 - > NULL
我々は総nmのステップがあってもよいため、見ることができ、ノード3はノード1への最初のステップバックである、第二のステップは、4点のノード1に戻って負担します。この操作は、その後、我々はそのような始まりとしてライン、上、ノード1、CURポイントノード2を指し事前と言う、そして我々は我々が使用することをノード3(ノートを指して、一時的なノードtを設定し、非常に規則的です一時変数ノードが第一切断するノードとルールダウンとして使用することができる前のノード)との間のリンクで保持し、その後、我々は、ノード2とノード3、ノードを切断2接合点に接続された次の4、即ちcur->次= T->次に、その後、ノード3はノード1の前面の背後の接続点に接続されている(すなわち、ノード2)、すなわち、T->次=プレ>次に、元のノード1とノード3、ノード2、ノード1の最終的な切断、すなわちプレ>次に= T。したがって、我々は3が除去されたノードを完了した、リアノード1が追加されます。第2のステップは、ノード1をリアに追加され、ノード4が削除され、同じ操作で、次のコードを参照してください、バーの下にそれを自分で試してみてください言っても過言ではありません。
/**
* 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;
}
}
反復法2
基本的にはインターバルの背面に置くと同じ考え方は、逆にします
/**
* 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;
}
}
再帰
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。ペアでスワップノード
反復法
/**
* 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;
}
}
再帰的な言葉遣い
/**
* 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。K-グループ内のノードを逆に
反復1の文言:スタックベースの二次記憶装置
/**
* 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;
}
}
反復2の文言:最初のリストの長さをカウントしなければなりません
/**
* 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;
}
}