- import java.util.HashMap;
- import java.util.Scanner;
- import java.util.Stack;
-
- public class LinkedListSummary {
-
- public static class Node{
- int value;
- Node next;
- public Node(int n){
- this.value=n;
- this.next=null;
- }
- }
- public static void main(String[] args) {
-
- Scanner in=new Scanner(System.in);
- Node head=null;
- if(in.hasNextInt()){
- head=new Node(in.nextInt());
- }
- Node temp=head;
- while(in.hasNextInt()){
- temp.next=new Node(in.nextInt());
- temp=temp.next;
- }
- in.close();
-
-
-
-
-
-
-
-
-
-
- }
-
- public static int getListLength(Node head){
- int len=0;
- while(head!=null){
- len++;
- head=head.next;
- }
- return len;
- }
-
- public static Node reverseList(Node head){
- if(head==null||head.next==null)return head;
- Node pre=null;
- Node nex=null;
- while(head!=null){
- nex=head.next;
- head.next=pre;
- pre=head;
- head=nex;
- }
- return pre;
- }
-
- public static Node reverseListRec(Node head){
- if(head==null||head.next==null)return head;
- Node reHead=reverseListRec(head.next);
- head.next.next=head;
- head.next=null;
- return reHead;
- }
-
- public static Node reGetKthNode(Node head,int k){
- if(head==null)return head;
- int len=getListLength(head);
- if(k>len)return null;
- Node target=head;
- Node nexk=head;
- for(int i=0;i<k;i++){
- nexk=nexk.next;
- }
- while(nexk!=null){
- target=target.next;
- nexk=nexk.next;
- }
- return target;
- }
-
- public static Node getMiddleNode(Node head){
- if(head==null||head.next==null)return head;
- Node target=head;
- Node temp=head;
- while(temp!=null&&temp.next!=null){
- target=target.next;
- temp=temp.next.next;
- }
- return target;
- }
-
- public static void reversePrintListRec(Node head){
- if(head==null)return;
- else{
- reversePrintListRec(head.next);
- System.out.println(head.value);
- }
- }
-
- public static void reversePrintListStack(Node head){
- Stack<Node> s=new Stack<Node>();
- while(head!=null){
- s.push(head);
- head=head.next;
- }
- while(!s.isEmpty()){
- System.out.println(s.pop().value);
- }
- }
-
- public static Node mergeSortedList(Node head1,Node head2){
- if(head1==null)return head2;
- if(head2==null)return head1;
- Node target=null;
- if(head1.value>head2.value){
- target=head2;
- head2=head2.next;
- }
- else{
- target=head1;
- head1=head1.next;
- }
- target.next=null;
- Node mergeHead=target;
- while(head1!=null && head2!=null){
- if(head1.value>head2.value){
- target.next=head2;
- head2=head2.next;
- }
- else{
- target.next=head1;
- head1=head1.next;
- }
- target=target.next;
- target.next=null;
- }
- if(head1==null)target.next=head2;
- else target.next=head1;
- return mergeHead;
- }
-
- public static Node mergeSortedListRec(Node head1,Node head2){
- if(head1==null)return head2;
- if(head2==null)return head1;
- if(head1.value>head2.value){
- head2.next=mergeSortedListRec(head2.next,head1);
- return head2;
- }
- else{
- head1.next=mergeSortedListRec(head1.next,head2);
- return head1;
- }
- }
-
- public static Node listSort(Node head){
- Node nex=null;
- if(head==null||head.next==null)return head;
- else if(head.next.next==null){
- nex=head.next;
- head.next=null;
- }
- else{
- Node mid=getMiddleNode(head);
- nex=mid.next;
- mid.next=null;
- }
- return mergeSortedList(listSort(head),listSort(nex));
- }
-
- public Node insertionSortList(Node head) {
- if(head==null||head.next==null)return head;
- Node pnex=head.next;
- Node pnex_nex=null;
- head.next=null;
- while(pnex!=null){
- pnex_nex=pnex.next;
- Node temp=head;
- Node temp_pre=null;
- while(temp!=null){
- if(temp.value>pnex.value)break;
- temp_pre=temp;
- temp=temp.next;
- }
- if(temp_pre==null){
- head=pnex;
- pnex.next=temp;
- }
- else{
- temp_pre.next=pnex;
- pnex.next=temp;
- }
- pnex=pnex_nex;
- }
- return head;
- }
-
- public static boolean hasCycle(Node head){
- boolean flag=false;
- Node p1=head;
- Node p2=head;
- while(p1!=null&&p2!=null){
- p1=p1.next;
- p2=p2.next.next;
- if(p2==p1){
- flag=true;
- break;
- }
- }
- return flag;
- }
-
-
- public static Node isIntersect(Node head1,Node head2){
- Node target=null;
- if(head1==null||head2==null)return target;
- int len1=getListLength(head1);
- int len2=getListLength(head2);
- if(len1>=len2){
- for(int i=0;i<len1-len2;i++){
- head1=head1.next;
- }
- }else{
- for(int i=0;i<len2-len1;i++){
- head2=head2.next;
- }
- }
- while(head1!=null&&head2!=null){
- if(head1==head2){
- target=head1;
- break;
- }
- else{
- head1=head1.next;
- head2=head2.next;
- }
- }
- return target;
- }
-
- public static Node getFirstNodeInCycleHashMap(Node head){
- Node target=null;
- HashMap<Node,Boolean> map=new HashMap<Node,Boolean>();
- while(head!=null){
- if(map.containsKey(head))target=head;
- else{
- map.put(head, true);
- }
- head=head.next;
- }
- return target;
- }
-
-
- public static Node getFirstNodeInCycle(Node head){
- Node fast=head;
- Node slow=head;
- while(fast!=null&&fast.next!=null){
- slow=slow.next;
- fast=fast.next.next;
- if(slow==fast)break;
- }
- if(fast==null||fast.next==null)return null;
-
- slow=head;
- while(slow!=fast){
- slow=slow.next;
- fast=fast.next;
- }
- return slow;
-
- }
-
-
- public static void deleteNode(Node head,Node delete){
-
- if(delete==null)return;
- if(delete.next==null){
- if(head==delete)head=null;
- else{
- Node temp=head;
- while(temp.next!=delete){
- temp=temp.next;
- }
- temp.next=null;
- }
- }
- else{
- delete.value=delete.next.value;
- delete.next=delete.next.next;
- }
- return;
- }
- }
单链表实现及其基本操作
原文:http://www.cnblogs.com/yinfj/p/7120339.html