javaシングルチェーン表逆順用法コード例
このブログは、比較的簡単です。シングルチェーン表の逆順がよく分かりません。見てください。
逆順思想
現在の休暇にはチェーンがあります。逆順操作が必要です。まず私たちが考えているのは、その針を逆順にすればいいということです。
実は、このようです。ブロガーはこれを目標にして作られたシングルチェーンの逆順操作です。初の逆順:
最初はpre、postはnullに設定されています。これは必要です。head.next=preのこの行のコードが実行された後、私達の元のheadノードのnextはnullになります。つまり、チェーン全体のnullです。
想像してみても、元のチェーンの一番後ろのnextもnullではないですか?ここの道理は一致している。
この時、preを更新するのは元のheadノードで、次の逆順に準備するためで、headも自然に元のhead.nextになりました。連続逆順。
すみません、手が震えて、間違えました。皆さん、ご了承ください。手描き図上の第5回の概略preノードはノード5の位置にあるべきで、headがない。
凡例からもわかりますが、私たちは毎回ヘッドを後ろに移動しながらpreノードを更新して逆順の効果を達成します。
コード
テストしたところ、コードの出力が正しいです。
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
ヘルプの理解は、次の例である。
0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 3 2 1 0
締め括りをつける
以上が、Javaシングルチェーンの逆順用法コードの例についての全部の内容です。興味のある方は引き続き当駅の他のテーマを参照してください。友達のサポートに感謝します。
逆順思想
現在の休暇にはチェーンがあります。逆順操作が必要です。まず私たちが考えているのは、その針を逆順にすればいいということです。
実は、このようです。ブロガーはこれを目標にして作られたシングルチェーンの逆順操作です。
Node pre = null;
Node post = null;
while(head!=null){
post = head.next;
head.next = pre;
pre = head;
head = post;
}
これが逆順の核心です。これから、私たちは一歩ずつ説明します。想像してみても、元のチェーンの一番後ろのnextもnullではないですか?ここの道理は一致している。
この時、preを更新するのは元のheadノードで、次の逆順に準備するためで、headも自然に元のhead.nextになりました。
すみません、手が震えて、間違えました。皆さん、ご了承ください。手描き図上の第5回の概略preノードはノード5の位置にあるべきで、headがない。
凡例からもわかりますが、私たちは毎回ヘッドを後ろに移動しながらpreノードを更新して逆順の効果を達成します。
コード
package list;
public class ReverseList {
public static void main(String[] args) {
Node head = new Node(1);
int[] value = {2,3,4,5};
Node temp = head;
for (int i = 0 ; i< value.length;i++) {
Node node = new Node(value[i]);
temp.next = node;
temp = temp.next;
}
printList(head);
//
head = reverse(head);
printList(head);
//
head = reverseSingleList(head);
printList(head);
}
public static void printList(Node head) {
while(head!=null) {
System.out.print("\t"+head.value);
head = head.next;
}
System.out.println();
}
public static Node reverse(Node head) {
Node pre = null;
Node post = null;
while(head!=null) {
post = head.next;
head.next = pre;
pre = head;
head = post;
}
return pre;
}
public static Node reverseSingleList(Node head) {
Node pre = null;
Node next = null;
while(head!=null) {
next = head.next;
head.next = pre;
pre = head;
head = next;
}
return pre;
}
}
class Node {
public int value;
public Node next;
public Node(int value) {
this.value = value;
}
}
テストテストしたところ、コードの出力が正しいです。
1 2 3 4 5
5 4 3 2 1
1 2 3 4 5
ヘルプの理解は、次の例である。
/**
* java
* @author Administrator
*
*/
public class SingleLinkedReverse {
class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}
public static void main(String[] args) {
SingleLinkedReverse slr = new SingleLinkedReverse();
Node head, tail;
head = tail = slr.new Node(0);
for (int i=1; i<10; i++){
Node p = slr.new Node(i);
tail.next = p;
tail = p;
}
tail = head;
while(tail != null){
System.out.print(tail.data+" ");
tail = tail.next;
}
head = reverse(head);
System.out.println(" ");
while(head != null){
System.out.print(head.data+" ");
head = head.next;
}
}
private static Node reverse(Node head) {
Node p1,p2 = null;
p1 = head;
while(head.next != null){
p2 = head.next;
head.next = p2.next;
p2.next = p1;
p1 = p2;
}
return p2;
}
}
テスト結果:0 1 2 3 4 5 6 7 8 9
9 8 7 6 5 3 2 1 0
締め括りをつける
以上が、Javaシングルチェーンの逆順用法コードの例についての全部の内容です。興味のある方は引き続き当駅の他のテーマを参照してください。友達のサポートに感謝します。