剑指offer题三

package jianzhioffer;

import java.util.ArrayList;
import java.util.Collections;

/**
 * 单链表的插入,删除,查找,遍历操作
 *
 */

//节点类
class ListNode {
    int val; //数据域
    ListNode next = null; //指针域

    ListNode(int val) {
        this.val = val;
    }
    
    //显示此节点
    void display(){
    	System.out.println(val +" ");
    }
}

//单链表
public class LinkList {
	ListNode first;//定义一个头节点
	int pos = 0; //节点的位置
	
	public LinkList(){
		this.first = null;
	}

	//插入一个头节点
	public void addFirstNode(int val){
		ListNode listnode = new ListNode(val);
		listnode.next = first;
		first = listnode;
	}
	
	//删除一个头节点,并返回头节点
	public ListNode deleteFirstNode(){
		ListNode temp = first;
		first = temp.next;
		return temp;
	}
	
	//在任意位置插入节点,在index的后面插入
	public void add(int index,int val){
		ListNode listnode = new ListNode(val);
		ListNode current = first;
		ListNode previous = first;
		while(pos != index){
			previous = current;
			current = current.next;
			pos++;
		}
		listnode.next = current;
		previous.next = listnode;
		pos = 0;
	}
	
	//删除任意位置的节点
	public ListNode deleteByPos(int index){
		ListNode current = first;
		ListNode previous = first;
		while(pos != index){
			pos++;
			previous = current;
			current = current.next;
		}
		if(current == first){
			first = first.next;
		}else{
			pos = 0;
			previous.next = current.next;
		}
		return current;
	}
	
	//根据节点的val删除节点(仅删除第一个)
	public ListNode deleteByVal(int data){
		ListNode current = first;
		ListNode previous = first; //记住上一个节点
		while(current.val != data){
			if(current.next ==null){
				return null;
			}
			previous = current;
			current = current.next;
		}
		if(current == first){
			first = first.next;
		}else{
			previous.next = current.next;
		}
		return current;
	}
	
	//显示出所有的节点信息
	public void displayAllNodes(){
		ListNode current = first;
		while(current!=null){
			current.display();
			current = current.next;
		}
	}
	
	//根据位置查找节点信息
	public ListNode findByPos(int index){
		ListNode current = first;
		if(pos!=index){
			current = current.next;
			pos++;
		}
		return current;
	}
	
	//根据数据查找节点信息
	public ListNode fingByData(int data){
		ListNode current = first;
		while(current.val != data){
			if(current.next == null){
				return null;
			}
			current = current.next;
		}
		return current;
	}
	
	//从尾到头反向打印每个节点的值
	/*ArrayList<Integer> arrayList=new ArrayList<Integer>();
	public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    	if(listNode != null){
    		this.printListFromTailToHead(listNode.next);
    		arrayList.add(listNode.val);
    	}
    	return arrayList;
    }*/
	public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
		ArrayList<Integer> list=new ArrayList<Integer>();
		while(listNode != null){
			list.add(listNode.val);
			listNode=listNode.next;
    	}
		Collections.reverse(list); //使用Collections的reverse方法,直接将list反转
    	return list;
	}
    
	// 输入一个链表,反转链表后,输出链表的所有元素。
	/*public ListNode ReverseList(ListNode head) {
		if(head == null){
			return null;
		}
		//当前节点是head,pre为当前节点的前一节点,next为当前节点的下一节点
        //需要pre和next的目的是让当前节点从pre->head->next1->next2变成pre<-head next1->next2
        //即pre让节点可以反转所指方向,但反转之后如果不用next节点保存next1节点的话,此单链表就此断开了
        //所以需要用到pre和next两个节点
        //1->2->3->4->5
        //1<-2<-3 4->5
		ListNode pre = null;
	    ListNode next = null;
	    while (head != null) {
	   	    //做循环,如果当前节点不为空的话,始终执行此循环,此循环的目的就是让当前节点从指向next到指向pre
	        //如此就可以做到反转链表的效果
	        //先用next保存head的下一个节点的信息,保证单链表不会因为失去head节点的原next节点而就此断裂
	        next = head.next;
	        //保存完next,就可以让head从指向next变成指向pre了,代码如下
	        head.next = pre;
	        //head指向pre后,就继续依次反转下一个节点
	        //让pre,head,next依次向后移动一个节点,继续下一次的指针反转
	        pre = head;
	        head = next;
	    }
	    //如果head为null的时候,pre就为最后一个节点了,但是链表已经反转完毕,pre就是反转后链表的第一个节点
	    //直接输出pre就是我们想要得到的反转后的链表
	    return pre;
	}*/

	public ListNode ReverseList(ListNode head) { //带头节点的反转链表
		if(head == null){
			return null;
		}
		ListNode tmp = null;
	    ListNode p = null;
	    
	    tmp = head.next;
	    while(tmp.next!= null){
	    	p = tmp.next;
	    	tmp.next = p.next;
	    	p.next = head.next;
	    	head.next = p;
	    }
	    return head;
	    
	}
    
	public static void main(String[] args) {
		LinkList linklist = new LinkList();
		linklist.addFirstNode(10);
		linklist.addFirstNode(9);
		linklist.addFirstNode(8);
		//8,9,10
		linklist.add(1, 7);
		linklist.add(2, 4);
		linklist.add(3, 66);
		//8,7,4,66,9,10
		linklist.displayAllNodes();
		
//		ListNode listnode = linklist.deleteFirstNode();
//		System.out.println("listnode:"+ listnode.val);
//		linklist.displayAllNodes();
		
//		listnode = linklist.deleteByPos(2);
//		System.out.println("listnode:"+ listnode.val);
//		linklist.displayAllNodes();
//		
//		listnode = linklist.deleteByVal(8);
//		System.out.println("listnode:"+ listnode.val);
//		linklist.displayAllNodes();

		ListNode node1 = linklist.findByPos(0);
		System.out.println("node1:" + node1.val);
		ListNode node2 = linklist.fingByData(9);
		System.out.println("node1:" + node2.val);
		
//		ArrayList<Integer> list = linklist.printListFromTailToHead(listnode);
//		System.out.println(list.toString());
		
		ListNode first = new ListNode(0); 
		first.next = linklist.findByPos(0);  //创建一个头节点,指向链表的第一个节点
		ListNode nodelist = linklist.ReverseList(first); //反转传入头节点的信息,最后返回一个ListNode的链表
		while(nodelist.next != null){
			nodelist.display();
			nodelist = nodelist.next;
		}
		
		
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值