java-实现链表反转-递归和非递归实现


对链表中部分节点进行反转操作,这些节点相隔k个:
0->1->2->3->4->5->6->7->8->9
k=2
8->1->6->3->4->5->2->7->0->9
注意1 3 5 7 9 位置是不变的。
解法:
将链表拆成两部分:
a.0->2->4->6->8
b.1->3->5->7->9
将a部分反转,再将a和b合并
==update end==
Java代码 复制代码  收藏代码
  1. public class LinkListReversing {  
  2.   
  3.       
  4.     public static void main(String[] args) {  
  5.         LinkList list=new LinkList();  
  6.         int[] a={1,2,3,4,5};  
  7.         for(int each:a){  
  8.             list.add(each);  
  9.         }  
  10.         list.display();  
  11.         list.reverse();  
  12.         list.display();  
  13.         list.reverseRecursive(list.getFirstNode());  
  14.         list.display();  
  15.     }  
  16.   
  17. }  
  18.   
  19.   
  20. class LinkList{  
  21.       
  22.     private Node firstNode;  
  23.     private int length;  
  24.       
  25.     public LinkList(){  
  26.         clear();  
  27.     }  
  28.       
  29.     public void clear(){  
  30.         firstNode=null;  
  31.         length=0;  
  32.     }  
  33.       
  34.     public Node getFirstNode(){  
  35.         return firstNode;  
  36.     }  
  37.       
  38.     public boolean add(int data){  
  39.         Node node=new Node(data);  
  40.         if(isEmpty()){  
  41.             firstNode=node;  
  42.         }else{  
  43.             Node lastNode=getNodeAt(length);  
  44.             lastNode.next=node;  
  45.         }  
  46.         length++;  
  47.         return true;  
  48.     }  
  49.       
  50.     public boolean isEmpty(){  
  51.         //return length==0;  
  52.         //use assert to get more details when error occurs  
  53.         boolean result;  
  54.         if(length==0){  
  55.             assert firstNode==null;  
  56.             result=true;  
  57.         }else{  
  58.             assert firstNode!=null;  
  59.             result=false;  
  60.         }  
  61.         return result;  
  62.     }  
  63.       
  64.     public void reverse(){  
  65.         if(firstNode==null)return;  
  66.         Node p=firstNode;//use p to traverse every node  
  67.         Node previous=null;  
  68.         while(p.next!=null){  
  69.             Node q=p.next;// save p.next first because the next sentence changes p.next  
  70.             p.next=previous;  
  71.             previous=p;  
  72.             p=q;  
  73.         }  
  74.         p.next=previous;  
  75.         firstNode=p;//should not be ignored  
  76.     }  
  77.       
  78.     //recursive  
  79.     public Node reverseRecursive(Node p){  
  80.         if(p==null)return null;  
  81.         if(p.next==null){  
  82.             firstNode=p;  
  83.             return p;  
  84.         }  
  85.         Node q=p.next;  
  86.         //reverse the remaining nodes,except p  
  87.         //when recursive returns,you can regard the link as a link that has just two elements: p-->q  
  88.         //so the last reversing is simple: q.next=p;p.next=null;  
  89.         Node ph=reverseRecursive(q);  
  90.         q.next=p;  
  91.         p.next=null;  
  92.         System.out.print("("+ph.data+")");//ph.data=1,always  
  93.         return ph;  
  94.     }  
  95.       
  96.     public void display(){  
  97.         Node node=firstNode;  
  98.         while(node!=null){  
  99.             System.out.print(node.data+" ");  
  100.             node=node.next;  
  101.         }  
  102.         System.out.println();  
  103.     }  
  104.     private Node getNodeAt(int position){  
  105.         Node re=firstNode;  
  106.         if(!isEmpty()&&position>1&&position<=length){  
  107.             for(int count=1;count<position;count++){  
  108.                 re=re.next;  
  109.             }  
  110.         }  
  111.         return re;  
  112.     }  
  113.       
  114.     //use inner class  
  115.     private class Node{  
  116.         private int data;  
  117.         private Node next;  
  118.           
  119.         private Node(int data){  
  120.             this.data=data;  
  121.             next=null;  
  122.         }  
  123.     }  
  124. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值