链表的相关操作

文章主要是对链表进行一些操作,下面是相关代码:

ListNode类

package com.cs.algorithm.algorithm;

public class ListNode {

	public ListNode() {
		
	}
	
	public ListNode(int val, ListNode nextnode) {
		super();
		this.val = val;
		this.nextnode = nextnode;
	}
	private int val;
	private ListNode nextnode;
	public int getVal() {
		return val;
	}
	public void setVal(int val) {
		this.val = val;
	}
	public ListNode getNextnode() {
		return nextnode;
	}
	public void setNextnode(ListNode nextnode) {
		this.nextnode = nextnode;
	}
	
	
}
package com.cs.algorithm.algorithm;

import java.util.HashMap;
import java.util.Map;

public class App 
{
	
	public static ListNode getList(int count) {
		
		ListNode head = new ListNode();
	    ListNode r = head;
		
		for(int i = 0; i < count; i++) {
			
			ListNode node = new ListNode();
		    //node.setVal(new Random().nextInt(count));
			node.setVal(i);
		    
		    r.setNextnode(node);
		    r = node;
		    
		}
		
		return head;
	}
	
    public static void scanList(ListNode node) {
		
    	if(null  == node) return;
		while(node.getNextnode() != null) {
			
			node = node.getNextnode();
			System.out.println(node.getVal()+"");
		}
	}
    
    //链表普通反转
    public static void reserveList(ListNode node) {
    	
    	if(null == node || null == node.getNextnode()) return;
    	ListNode head = node;
    	ListNode pre = null;
    	ListNode next = null;
    	node = node.getNextnode();
    	
    	while(node.getNextnode() != null) {
    		
    		next = node.getNextnode();
    		node.setNextnode(pre);
    		pre = node;
    		node = next;
    	}
    	
    	node.setNextnode(pre);
    	head.setNextnode(node);
    	
    	node = head;
    }
    
    //链表递归反转
    public static ListNode reserveOtherList(ListNode head,ListNode node) {
    	
    	if(null == node) {
    		return node;
    	}
    	
    	if(node.getNextnode() == null) {
    		head.setNextnode(node);
    	}
    	ListNode temp = reserveOtherList(head,node.getNextnode());
    	
    	if(temp != null) {
    		
    		//if(temp.getNextnode() != null) {
    			temp.setNextnode(node);
    		//}
    	}
    	node.setNextnode(null);
    	
    	return node;
    }
	
    //链表中环的检测
    public static boolean checkCircle(ListNode node) {
    	
    	boolean bret = false;
    	if(null  == node) return bret;
    	
    	node = node.getNextnode();
    	
    	Map<ListNode,String> map = new HashMap<>();
		while(node != null) {
			
			if(map.containsKey(node)) {
				
				bret = true;
				break;
			}
			
			map.put(node, "1");
			node = node.getNextnode();
            
		}
		
		return bret;
    }
    
    //链表中环的检测
    public static boolean checkOtherCircle(ListNode node) {
    	
    	boolean bret = false;
    	if(null  == node) return bret;
    	
    	ListNode fastnode = node.getNextnode(),slownode = node;
    	
		while(slownode != null) {
			
			if(null == fastnode) {
				bret = false;
				break;
			}
            if(slownode == fastnode) {
				
				bret = true;
				break;
			}
            
			slownode = slownode.getNextnode();
			fastnode = fastnode.getNextnode().getNextnode();
            
		}
		
		return bret;
    }
    
    //两个有序链表合并
    public static ListNode combinList(ListNode firstnode,ListNode secondnode) {
    	
    	ListNode head = new ListNode();
    	ListNode curhead = head;
    	
    	ListNode ohead = firstnode;
    	firstnode = firstnode.getNextnode();
    	ohead.setNextnode(null);
    	
    	ohead = secondnode;
    	secondnode = secondnode.getNextnode();
    	ohead.setNextnode(null);
    	
    	while(firstnode != null && secondnode != null) {
    		
    		if(firstnode.getVal() < secondnode.getVal()) {
    			
    			head.setNextnode(firstnode);
    			
    			head = firstnode;
    			
    			firstnode = firstnode.getNextnode();
    			
    		}else if(firstnode.getVal() > secondnode.getVal()){
    			
    			head.setNextnode(secondnode);
    			
    			head = secondnode;
    			secondnode = secondnode.getNextnode();
    		}else {
    			
    			ListNode firstnext = firstnode.getNextnode();
    			ListNode secondnext = secondnode.getNextnode();
    			
    			head.setNextnode(firstnode);
    			firstnode.setNextnode(secondnode);
    			head = secondnode;
    			firstnode = firstnext;
    			secondnode = secondnext;
    		}
    		head.setNextnode(null);
    	}
    	
    	if(firstnode != null) {
    		
    		head.setNextnode(firstnode);
    	}else if(secondnode != null) {
    		
    		head.setNextnode(secondnode);
    	}
    	return curhead;
    }
    
    //删除链表中倒数第n个节点
    /*
     * 思路:
     * 两个指针,一个先走n-1步(从head开始),然后一起走,先走的指针到了尾部,那么后走的指针所在的位置就是要求的节点
     * 为什么是这样,可以这样想,针对后走的指针,要保证先走的指针刚到尾部,后走的指针刚好到达指定的点,后走的点离尾点
     * 相差的就是先走的指针领先的步伐。
     */
    public static void deletePosNode(ListNode node,int n) {
    	
    	ListNode flow = node;
    	ListNode fast = node;
    	ListNode preflow = node;
    	
    	int ncount = 0;
    	while(fast.getNextnode() != null) {
    		
    		if(ncount++ >= n - 1) {
    			
    			preflow = flow;
    			flow = flow.getNextnode();
    		}
    		fast = fast.getNextnode();
    	}
    	//此时flow所在的点就是要删除的点
    	preflow.setNextnode(flow.getNextnode());
    	flow = null;
    	
    }
    
    //获取链表的中间节点
    public static ListNode getMidleNode(ListNode node) {
    	
    	ListNode flow = node.getNextnode();
    	ListNode fast = node.getNextnode();
    	
    	while(fast != null && fast.getNextnode() != null) {
    		
    		flow = flow.getNextnode();
    		fast = fast.getNextnode().getNextnode();
    	}
    	
        
    	return flow;
    }
    
    public static void main( String[] args )
    {
        
    	ListNode node = getList(10);
    	//遍历链表
    	scanList(node);
    	
    	System.out.println("链表反转后");
    	//反转链表
    	/*reserveList(node);
    	scanList(node);*/
    	
    	//递归反转链表
    	reserveOtherList(node,node.getNextnode());
    	scanList(node);
    	
    	//node.getNextnode().getNextnode().getNextnode().setNextnode(node);
    	
    	if(checkCircle(node)) {
    		
    		System.out.println("存在环");
    	}else {
    		System.out.println("不存在环");
    	}
    	
    	ListNode deletenode = getList(10);
    	System.out.println("删除点前的链表为:");
    	scanList(deletenode);
    	System.out.println("中间节点值为"+getMidleNode(deletenode).getVal());
    	deletePosNode(deletenode,5);
    	System.out.println("删除点后的链表为:");
    	scanList(deletenode);
    	System.out.println("中间节点值为"+getMidleNode(deletenode).getVal());
    	deletePosNode(deletenode,2);
    	System.out.println("删除点后的链表为:");
    	scanList(deletenode);
    	System.out.println("中间节点值为"+getMidleNode(deletenode).getVal());
    	System.out.println("--------------------");
    	//ListNode node1 = getList(10);
    	//ListNode node2 = getList(10);
    	ListNode node10 = new ListNode(19,null);
    	ListNode node11 = new ListNode(12,node10);
    	ListNode node12 = new ListNode(9,node11);
    	ListNode node13 = new ListNode(5,node12);
    	ListNode node14 = new ListNode(1,node13);
    	ListNode node15 = new ListNode(0,node14);
    	
    	ListNode node22 = new ListNode(17,null);
    	ListNode node23 = new ListNode(16,node22);
    	ListNode node24 = new ListNode(8,node23);
    	ListNode node25 = new ListNode(4,node24);
    	ListNode node26 = new ListNode(2,node25);
    	ListNode node27 = new ListNode(0,node26);
    	
    	scanList(combinList(node15,node27));
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值