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);
}
}