生活随笔
收集整理的這篇文章主要介紹了
数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
數據結構與算法之打印兩個有序鏈表公共部分和判斷一個鏈表是否具有回文結構
目錄
打印兩個有序鏈表公共部分判斷一個鏈表是否具有回文結構
1. 打印兩個有序鏈表公共部分
1.問題描述
思路:Node1和Node2的value進行比較,小的一方向后移一位,等于則打印,然后兩節點后移一位再比較。
代碼實現
public class Code_PrintCommonPart {public static class Node {public int value
;public Node next
;public Node(int data
) {this.value
= data
;}}public static void printCommonPart(Node head1
, Node head2
) {System
.out
.print("Common Part: ");while (head1
!= null
&& head2
!= null
) {if (head1
.value
< head2
.value
) {head1
= head1
.next
;} else if (head1
.value
> head2
.value
) {head2
= head2
.next
;} else {System
.out
.print(head1
.value
+ " ");head1
= head1
.next
;head2
= head2
.next
;}}System
.out
.println();}public static void printLinkedList(Node node
) {System
.out
.print("Linked List: ");while (node
!= null
) {System
.out
.print(node
.value
+ " ");node
= node
.next
;}System
.out
.println();}public static void main(String
[] args
) {Node node1
= new Node(2);node1
.next
= new Node(3);node1
.next
.next
= new Node(5);node1
.next
.next
.next
= new Node(6);Node node2
= new Node(1);node2
.next
= new Node(2);node2
.next
.next
= new Node(5);node2
.next
.next
.next
= new Node(7);node2
.next
.next
.next
.next
= new Node(8);printLinkedList(node1
);printLinkedList(node2
);printCommonPart(node1
, node2
);}
}
編譯結果
2. 判斷一個鏈表是否具有回文結構
思路:創建一個棧,將鏈表元素依次放進去,再取出一個一個比較即可。
代碼實現
import java
.util
.Stack
;public class Code_IsPalindromeList {public static class Node {public int value
;public Node next
;public Node(int data
) {this.value
= data
;}}public static boolean isPalindrome1(Node head
) {Stack
<Node> stack
= new Stack<Node>();Node cur
= head
;while (cur
!= null
) {stack
.push(cur
);cur
= cur
.next
;}while (head
!= null
) {if (head
.value
!= stack
.pop().value
) {return false;}head
= head
.next
;}return true;}public static boolean isPalindrome2(Node head
) {if (head
== null
|| head
.next
== null
) {return true;}Node right
= head
.next
;Node cur
= head
;while (cur
.next
!= null
&& cur
.next
.next
!= null
) {right
= right
.next
;cur
= cur
.next
.next
;}Stack
<Node> stack
= new Stack<Node>();while (right
!= null
) {stack
.push(right
);right
= right
.next
;}while (!stack
.isEmpty()) {if (head
.value
!= stack
.pop().value
) {return false;}head
= head
.next
;}return true;}public static boolean isPalindrome3(Node head
) {if (head
== null
|| head
.next
== null
) {return true;}Node n1
= head
;Node n2
= head
;while (n2
.next
!= null
&& n2
.next
.next
!= null
) { n1
= n1
.next
; n2
= n2
.next
.next
; }n2
= n1
.next
; n1
.next
= null
; Node n3
= null
;while (n2
!= null
) { n3
= n2
.next
; n2
.next
= n1
; n1
= n2
; n2
= n3
; }n3
= n1
; n2
= head
;boolean res
= true;while (n1
!= null
&& n2
!= null
) { if (n1
.value
!= n2
.value
) {res
= false;break;}n1
= n1
.next
; n2
= n2
.next
; }n1
= n3
.next
;n3
.next
= null
;while (n1
!= null
) { n2
= n1
.next
;n1
.next
= n3
;n3
= n1
;n1
= n2
;}return res
;}public static void printLinkedList(Node node
) {System
.out
.print("Linked List: ");while (node
!= null
) {System
.out
.print(node
.value
+ " ");node
= node
.next
;}System
.out
.println();}public static void main(String
[] args
) {Node head
= null
;printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(2);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(1);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(2);head
.next
.next
= new Node(3);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(2);head
.next
.next
= new Node(1);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(2);head
.next
.next
= new Node(3);head
.next
.next
.next
= new Node(1);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(2);head
.next
.next
= new Node(2);head
.next
.next
.next
= new Node(1);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");head
= new Node(1);head
.next
= new Node(2);head
.next
.next
= new Node(3);head
.next
.next
.next
= new Node(2);head
.next
.next
.next
.next
= new Node(1);printLinkedList(head
);System
.out
.print(isPalindrome1(head
) + " | ");System
.out
.print(isPalindrome2(head
) + " | ");System
.out
.println(isPalindrome3(head
) + " | ");printLinkedList(head
);System
.out
.println("=========================");}}
編譯結果
總結
以上是生活随笔為你收集整理的数据结构与算法之打印两个有序链表公共部分和判断一个链表是否具有回文结构的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。