Java与算法(4)

Java与算法(4)

题目:
给定两个有序链表的头指针head1和head2,打印两个链表的公共部分
public class CommonPart {
	
	public static class Node {
		int data;
		Node next;
		public Node(int data) {
			this.data = data;
		}
	}
	
	public void printLink(Node head) {
		while (head!=null) {
			System.out.println(head.data+" ");
			head=head.next;
		}
		System.out.println();
	}
	
	public void getCommenPart(Node h1,Node h2) {
		while (h1!=null&&h2!=null) {
			if (h1.data>h2.data) {
				h2=h2.next;
			} else if(h1.data<h2.data) {
				h1 = h1.next;
			} else {
				System.out.println(h1.data);
				h1 = h1.next;
				h2 = h2.next;
			}
		}
		System.out.println("");
	}
	
	
	public static void main(String[] args) {
			Node node1 = new Node(1);
			node1.next = new Node(3);
			node1.next.next = new Node(7);
			node1.next.next.next = new Node(8);
			node1.next.next.next.next = new Node(9);
			
			Node node2 = new Node(2);
			node2.next = new Node(3);
			node2.next.next = new Node(9);
			node2.next.next.next = new Node(10);
			node2.next.next.next.next = new Node(11);
			CommonPart commonPart =  new CommonPart();
			commonPart.printLink(node1);
			commonPart.printLink(node2);
			commonPart.getCommenPart(node1, node2);
			
	}

}
题目:
分别实现两个函数,一个可以删除单链表倒数第k个节点,另一个可以删除双链表倒数第k个节点。
public class DeleteLastSlink {
	public static class Node {
		int data;
		Node next;
		public Node(int data) {
					this.data = data;
		}
	}
	
	public Node deleteLast(Node head,int k) {
			if (head == null && k<1) {
				return head;
			}
			
			Node curnode = head;
			while (curnode!=null) {
				k--;
				curnode=curnode.next;
			}
			
			if (k==0) {
				 head=head.next;
			}
			if (k<0) {
				curnode = head;
				while (++k!=0) {
					curnode=curnode.next;
				}
				curnode.next = curnode.next.next;
				
			}
			
			return head;
	}
	
	public void print(Node head) {
		Node node = head;
		while (node!=null) {
			System.out.println(node.data);
			node=node.next;
		}
	}
	

	public static void main(String[] args) {
			Node head = new Node(1);
			head.next = new Node(5);
			head.next.next = new Node(10);
			head.next.next.next = new Node(25);
			head.next.next.next.next = new Node(30);
			DeleteLastSlink deleteLastSlink = new DeleteLastSlink();
			deleteLastSlink.print(head);
			System.out.println("-----");
			deleteLastSlink.deleteLast(head, 3);
			deleteLastSlink.print(head);
	}

}
public class DeleteLastDlink {
	public static class Node {
		int data;
		Node previous;
		Node next;
		public Node(int data) {
					this.data = data;
			}
	}
	
	public Node deleteLast(Node head,int k) {
			if (head == null && k<1) {
				return head;
			}
			Node curnode = head;
			while (curnode!=null) {
				k--;
				curnode = curnode.next;
			}
			if (k==0) {
				head = head.next;
			}
			if (k<0) {
				curnode = head;
				while (++k!=0) {
					curnode = curnode.next;
				}
				curnode.next.next.previous = curnode;
				curnode.next  = curnode.next.next;
			}
			return head;
		
	}
	public void print(Node head) {
		Node node = head;
		while (node!=null) {
			System.out.println(node.data);
			node=node.next;
		}
	}
	
	
	
	
	public static void main(String[] args) {
			Node head = new Node(1);
			head.next = new Node(3);
			head.next.next = new Node(5);
			head.next.next.next = new Node(7);
			head.next.next.next.next = new Node(9);
			DeleteLastDlink deleteLastDlink = new DeleteLastDlink();
			deleteLastDlink.print(head);
			System.out.println("-----------");
			deleteLastDlink.deleteLast(head, 3);
			deleteLastDlink.print(head);
	}

}
题目:
给定链表的头节点head,实现删除链表中间节点的函数。

如: 1-》2,删除1 1-》2-》3,删除2 1-》2-》3-》4,删除2

public class RemoveMidNode {
	public static class Node{
		int data;
		Node next;
		public Node(int data) {
				this.data = data;
		}
	}
	
	public Node removeMid(Node head) {
		if (head.next.next==null) {
			return head.next;
		}
		if (head==null && head.next==null) {
			return head;
		}
		
		Node pre = head;
		Node cur = head.next.next;
		while (cur.next!=null && cur.next.next!=null) {
			pre=pre.next;
			cur = cur.next.next;
		}
		pre.next = pre.next.next;
		return head;
		
	}
	
	public void print(Node head) {
		Node node = head;
		while (node!=null) {
			System.out.println(node.data);
			node=node.next;
		}
	}
	
	
	public static void main(String[] args) {
		Node head = new Node(1);
		head.next = new Node(3);
		head.next.next = new Node(5);
		head.next.next.next = new Node(7);
		head.next.next.next.next = new Node(9);
		RemoveMidNode removeMidNode = new RemoveMidNode();
		removeMidNode.print(head);
		System.out.println("-------------");
		removeMidNode.removeMid(head);
		removeMidNode.print(head);
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值