删除链表的中间节点和a/b处的节点
2017-10-27 15:46
375 查看
实现的代码如下:
//删除链表的中间节点和a/b处的节点
public class DelMidList{
//链表节点的定义
public static class Node{
public int value;
public Node next;
public Node(int data)
{
this.value=data;
}
}
//删除链表中间节点
public static int DelMidOfList(Node head)
{
if(head==null)
{
return Integer.MAX_VALUE; //表示不删除任何节点
}
Node tmp=head;
int leng=0; //记录链表的长度
while(tmp!=null)
{
++leng;
tmp=tmp.next;
}
System.out.println("链表的长度为:"+leng);
if(leng==1)
{
return head.value;
}
if(leng%2==0) //节点偶数处理方式
{
for(int i=0;i<leng/2-2;i++)
{
head=head.next;
}
}
else
{ //节点奇数处理方式
for(int i=0;i<(int)(leng/2)-1;i++)
{
head=head.next;
}
}
Node p=head.next; //要删除的中间节点
head.next=p.next; //删除中间节点
return p.value;
}
//删除链表中a/b处节点
public static int DelABOfList(Node head,int a,int b)
{
double r=(double)a/(double)b; //注意整型比较会向下取整 例如(1/5==0,2/5==0)
//System.out.println("a/b的值为:"+r);
if((head==null)|| (a<0)||(b<0))
{
return Integer.MAX_VALUE; //表示没有可删除的节点
}
if((r==0.0)||(r>1.0))
{
return Integer.MAX_VALUE; //表示没有可删除的节点
}
Node tmp=head;
int len=0; //记录链表的长度
while(tmp!=null)
{
++len;
tmp=tmp.next;
}
System.out.println("链表的长度为:"+len);
//删除第一个节点
if(r==1/(double)len)
{
Node p=head;
head=head.next;
return p.value;
}
for(int i=2;i<len;i++)
{
if(r>(double)i/(double)len)
{
head=head.next;
}
}
Node p=head.next; //要删除的中间节点
head.next=p.next; //删除中间节点
return p.value;
}
public static void main(String []args)
{
//Node mode=null;
Node node=new Node(1);
node.next=new Node(2);
node.next.next=new Node(3);
node.next.next.next=new Node(4);
node.next.next.next.next=new Node(5);
//node.next.next.next.next.next=new Node(6);
System.out.println("删除中间节点值: "+DelMidOfList(node));
Node mode=new Node(1);
mode.next=new Node(2);
mode.next.next=new Node(3);
mode.next.next.next=new Node(4);
mode.next.next.next.next=new Node(5);
System.out.println("删除链表的a/b的值为:"+DelABOfList(mode,1,5));
}
}
左神的代码:
public class Problem_03_RemoveNodeByRatio {
public static class Node {
public int value;
public Node next;
public Node(int data) {
this.value = data;
}
}
public static Node removeMidNode(Node head) {
if (head == null || head.next == null) {
return head;
}
if (head.next.next == null) {
return head.next;
}
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 static Node removeByRatio(Node head, int a, int b) {
if (a < 1 || a > b) {
return head;
}
int n = 0;
Node cur = head;
while (cur != null) {
n++;
cur = cur.next;
}
n = (int) Math.ceil(((double) (a * n)) / (double) b);
if (n == 1) {
head = head.next;
}
if (n > 1) {
cur = head;
while (--n != 1) {
cur = cur.next;
}
cur.next = cur.next.next;
}
return head;
}
public static void printLinkedList(Node head) {
System.out.print("Linked List: ");
while (head != null) {
System.out.print(head.value + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);
head.next.next.next = new Node(4);
head.next.next.next.next = new Node(5);
head.next.next.next.next.next = new Node(6);
printLinkedList(head);
head = removeMidNode(head);
printLinkedList(head);
head = removeByRatio(head, 2, 5);
printLinkedList(head);
head = removeByRatio(head, 1, 3);
printLinkedList(head);
}
}
相关文章推荐
- 建立一个带附加头结点的单链表.实现测长/打印/删除结点/插入结点/逆置/查找中间节点/查找倒数第k个节点/判断是否有环
- 2.3-删除链表中间节点
- cci-Q2.3 只给出中间节点,删除链表中间节点
- 链表问题(2)-- 删除聊表的中间节点和a/b处的节点
- 给定链表中间节点指针,删除中间节点的方法
- 删除链表的中间节点和a/b处节点
- 左程云_算法与数据结构 — 链表问题 — 03删除链表的中间节点和a/b处的节点
- 删除单链表的头元素;单链表,只是遍历一次,求出中间节点
- [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?
- 寻找和删除链表的中间节点
- [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?
- 删除链表的中间节点
- (转)问题:假设一个没有头指针的单链表。一个指针指向此单链表中间的一个节点(既不是第一个,也不是最后一个节点),请将该节点从单链表中删除。
- 删除链表中间节点
- 单链表删除头节点和不知道N的情况下寻找中间节点
- 删除链表的中间节点 / 删除链表a/b处的节点
- C++删除链表中间节点的方法
- 删除链表中一个位于中间位置的节点
- 给定链表中间节点指针,删除中间节点的方法
- 删除链表的中间节点(每日一道算法题)