删除链表的中间节点
来源:互联网 发布:疯狂java讲义百度云 编辑:程序博客网 时间:2024/06/05 15:34
package com.lyf.linkList;import org.junit.Test;/** * Created by fangjiejie on 2017/5/17. */public class RemoveMidNode<T> { public class Node<T>{ T value; Node<T> next; public Node(T value) { this.value = value; } public String toString(){ return value.toString(); } } public Node<T> removeMid(Node head){//删除中间节点就是要找到中间结点的前一个节点 if(head==null){ return head; } if(head.next==null){ return head; } Node<T> cur1=head; Node<T> cur2=cur1.next; while(cur2.next!=null&&cur2.next.next!=null){ cur1=cur1.next;//cur1以一倍的速度向后移动。 cur2=cur2.next.next;//cur2以二倍的速度向后移动。当cur2到达末尾时候,cur1也就达到了整个链表中间位置的前一个节点 } cur1.next=cur1.next.next; return head; } public Node<Integer> head; @Test public void test() { Node<Integer> []nodes=new Node[10]; for(int i=0;i<10;i++){ nodes[i]=new Node<>(i+10); if(i==0){ head=nodes[i]; }else{ nodes[i-1].next=nodes[i]; } } for(int i=0;i<10;i++){ System.out.print(nodes[i].toString()+","); } System.out.println(); Node<Integer> tmp=(Node<Integer>)removeMid(head); while(tmp!=null){ System.out.print(tmp.toString()+","); tmp=tmp.next; } }}
阅读全文
0 0
- 删除链表的中间节点
- 删除链表的中间节点
- 删除链表的中间节点
- 删除链表中间节点
- 删除链表中间节点
- 寻找和删除链表的中间节点
- 2.3-删除链表中间节点
- 链表问题---删除链表的中间节点和a/b处的节点
- 链表问题 删除链表的中间节点和a/b处的节点
- 删除链表的中间节点和a/b处的节点
- 链表问题(2)-- 删除聊表的中间节点和a/b处的节点
- 删除链表的中间节点和a/b处的节点
- 【链表】- 删除链表的中间节点和a/b处的节点
- 删除链表的中间节点和a/b处节点
- cci-Q2.3 只给出中间节点,删除链表中间节点
- [google面试CTCI] 2-3 只给定链表中间节点指针,如何删除中间节点?
- 链表之删除链表中间节点
- 单向循环链表的头插,尾插,中间插以及删除节点
- vector
- Error running app: Instant Run requires 'Tools | Android | Enable ADB integration' to be enabled
- html设置图像与文字的对齐方式
- .NET 类库研究必备参考 添加微软企业库源码
- 整理吧-Android测试
- 删除链表的中间节点
- jvm Local Variables ,Operand Stacks
- Qt下载地址
- MyEclipse初次使用的配置步骤
- html为图像添加提示文字
- map
- eclipse-web.xml is missing and <failOnMissingWebXml> is set to true问题处理
- 泛型边界控制Class<? extends Object>
- 调用CSDN的开发API——>博客接口——>获取博主基本信息【2】