Java 链表操作

来源:互联网 发布:我国农产品进出口数据 编辑:程序博客网 时间:2024/05/22 04:58

转自:http://www.jb51.net/article/71885.htm

 public class LinkList{ class Node{ int data; Node next; public Node(int data){ this.data=data; } }  public  Node head; public Node current;//方法:向链表中添加数据 public void add(int data){ if(head==null){//如果头节点为空,说明这个链表还没创建。把新的结点赋值给头节点 head=new Node(data); current=head;     }  else{ //创建新的节点,放在当前节点的后面 current.next=new Node(data); //把链表的当前索引向后移动一位 current=current.next; } } //方法:遍历链表(打印输出链表) public void print(Node node){ if(node==null){ System.out.print("Eorr"); return ; } current=node; while(current!=null){ System.out.println(current.data); current=current.next; } } //方法:获取单链表的长度 public int getLength(Node head){ if(head==null) return 0; int length=0; Node current=head; while(current!=null){ length++; current=current.next; } return length; } //查找单链表中的倒数第k个节点; //遍历 求出链表长度size public int findLastNode(int index){ if(head==null){ return -1; } int size=0; current=head; while(current!=null){ size++; current=current.next; } current=head; for(int i=0;i<size-index;i++){ current=current.next; } return  current.data; } //不遍历链表的长度 //声明两个指针first 和second//public Node findLastNode(Node head,int index){if(head==null)return null;Node first=head;Node second=head;//让second 节点向后挪index个位置for(int i=0;i<index;i++){second=second.next;} while(second!=null){first=first.next;second=second.next;}return first;}//再次改进 (考虑到k大于链表中节点个数时候抛出异常//1:k=0 2:k>sizepublic Node findLastNode_3(Node head ,int k){if(k==0||head==null)return null;Node first =head;Node second=head;//让second结点向后挪k-1个位置for(int i=0;i<k;i++){second=second.next;if(second==null){//k>sizereturn  null;}}while(second.next!=null){first=first.next;second=second.next;}return first;}//查找单链表中的中间节点public Node findMidNode(Node haed){if(head==null){System.out.println("Error");return null;}Node first=head;Node second=head;while(second!=null&&second.next!=null){first=first.next;second=second.next.next;}return  first;}//6 renturn 4//合并两个有序链表 合并后依然有序public Node mergeLinkList(Node head1,Node head2){if(head1==null&&head2==null)return null;if(head1==null){return head2;}if(head2==null)return head1;Node head;Node current;if(head1.data>head2.data){head=head1;current=head1;head1=head1.next;}else{head=head2;current=head2;head2=head2.next;}while(head1!=null&&head2!=null){if(head1.data<head2.data){current.next=head1;current=head1;head1=head1.next;}else{current.next=head2;current=head2;head2=head2.next;}}if(head1!=null){current.next=head1;}if(head2!=null){current.next=head2;}return  head;}//单链表的反转public Node reverseList(Node head){if(head==null||head.next==null)return head; Node current=head;Node next=null;Node reverseHead=null;while(current!=null){next=current.next;current.next=reverseHead;reverseHead=current;current=next;}return reverseHead;}public static void main(String[] args) {LinkList list1=new LinkList();//向LinkList中添加数据LinkList list2=new LinkList();for(int i=0;i<6;i++){list2.add(i);}list1.head=list2.reverseList(list2.head);list1.print(list1.head);for(int i=0;i<8;i++){list2.add(i);}LinkList a=new LinkList();a.head=a.mergeLinkList(list1.head,list2.head);a.print(a.head);}}


0 0
原创粉丝点击