java--删除链表偶数节点
public class ListNode {
int data;//当前节点的值
ListNode next = null;//是指向下一个节点的指针/引用
public ListNode(int data){
this.data = data;
}
}
public class DeleteListNode {
public static ListNode delete(ListNode head){
//链表为空
if(head == null){
return null;
}
ListNode odd = head;
//链表长度为偶数
if(length(head)%2 == 0){
while (odd.next != null && odd.next.next != null){
odd.next = odd.next.next;
odd = odd.next;
if(odd.next.next == null && odd.next != null){
odd.next=null;
}
}
}
//链表长度为奇数
if(length(head)%2 == 1){
while (odd.next != null && odd.next.next != null){
odd.next = odd.next.next;
odd = odd.next;
}
}
//返回头结点
return head;
}
//返回链表长度
private static int length(ListNode node){
int length = 0;
while (node != null){
node = node.next;
length++;
}
return length;
}
//打印链表
private static void print(ListNode node) {
System.out.print(node.data);
if (node.next != null){
print(node.next);
}
}
public static void main(String[] args){
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
ListNode node5 = new ListNode(5);
ListNode node6 = new ListNode(6);
ListNode node7 = new ListNode(7);
ListNode node8 = new ListNode(8);
ListNode node9 = new ListNode(9);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
node5.next = node6;
node6.next = node7;
node7.next = node8;
node8.next = node9;
System.out.println("删除偶数节点前,链表为:");
print(node1);
delete(node1);
System.out.println("\n删除偶数节点后,链表为:");
print(node1);
}
}
java--删除链表偶数节点的更多相关文章
- 剑指Offer:删除链表的节点【18】
剑指Offer:删除链表的节点[18] 题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3-& ...
- [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...
- 【链表】在O(1)的时间删除链表的节点
/** * 在O(1)的时间删除链表的节点 * * @author * */ public class Solution { public static void deleteNode(Node he ...
- 【剑指offer】面试题 18. 删除链表的节点
面试题 18. 删除链表的节点
- [剑指 Offer 18. 删除链表的节点]
[剑指 Offer 18. 删除链表的节点] 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点. 返回删除后的链表的头节点. 注意:此题对比原题有改动 示例 1: 输入: head ...
- [LeetCode] Delete Node in a Linked List 删除链表的节点
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- 删除链表中间节点和a/b处的节点
[题目]: 给定链表的头节点 head,实现删除链表的中间节点的函数. 例如: 步删除任何节点: 1->2,删除节点1: 1->2->3,删除节点2: 1->2->3-& ...
- 剑指offer——19删除链表的节点
题目一: 在O(1)时间内删除链表节点.给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 书本讲得不明就里 class Solution { void DeleteNode ...
- LeetCode 面试题18. 删除链表的节点
题目链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/ 给定单向链表的头指针和一个要删除的节点的值,定义一 ...
随机推荐
- 洛谷 P2678 [ NOIP 2015 ] 跳石头 —— 二分答案
题目:https://www.luogu.org/problemnew/show/P2678 二分答案. 代码如下: #include<iostream> #include<cstd ...
- javascript检测基本类型值或引用类型值的类型方法
首先javascript的数据类型分为两种数据类型:基本数据数据类型和引用数据类型 基本数据类型:Number,String,Boolean,Undefined,Null.原始值,是简单的数据段,可按 ...
- [Usaco2018 Open]Talent Show
Description FarmerJohn要带着他的N头奶牛,方便起见编号为1-N,到农业展览会上去,参加每年的达牛秀!他的第i头奶牛重量为wi,才艺水平为ti,两者都是整数.在到达时,Farmer ...
- G - And Then There Was One (约瑟夫环变形)
Description Let’s play a stone removing game. Initially, n stones are arranged on a circle and numbe ...
- pyinstaller打包报错:AttributeError: 'str' object has no attribute 'items'
导致原因和python多数奇奇怪怪的问题一样,依赖包的版本问题. 解决办法: 对setuptools这个包进行升级,链接在这里 https://pypi.org/project/setuptools/ ...
- 【Python精华】100个Python练手小程序
100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...
- SpringBoot-redis-session
配置pom <parent> <groupId>org.springframework.boot</groupId> <artifactId>sprin ...
- es6之数据结构 set,WeakSet,mapWeakMap
{ let list = new Set(); list.add(1); list.add(2); list.add(1); console.log(list); //Set(2) {1, 2} le ...
- 读《实战 GUI 产品的自动化测试》之:第二步,构建利于维护的自动化测试系统
转载自:http://www.ibm.com/developerworks/cn/rational/r-cn-guiautotesting2/ 基石——IBM 框架简介 Rational Functi ...
- Java屏幕截图及剪裁
Java标准API中有个Robot类,该类可以实现屏幕截图,模拟鼠标键盘操作这些功能.这里只展示其屏幕截图. 截图的关键方法createScreenCapture(Rectangle rect) ,该 ...