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--删除链表偶数节点的更多相关文章

  1. 剑指Offer:删除链表的节点【18】

    剑指Offer:删除链表的节点[18] 题目描述 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3-& ...

  2. [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 ...

  3. 【链表】在O(1)的时间删除链表的节点

    /** * 在O(1)的时间删除链表的节点 * * @author * */ public class Solution { public static void deleteNode(Node he ...

  4. 【剑指offer】面试题 18. 删除链表的节点

    面试题 18. 删除链表的节点

  5. [剑指 Offer 18. 删除链表的节点]

    [剑指 Offer 18. 删除链表的节点] 给定单向链表的头指针和一个要删除的节点的值,定义一个函数删除该节点. 返回删除后的链表的头节点. 注意:此题对比原题有改动 示例 1: 输入: head ...

  6. [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 ...

  7. 删除链表中间节点和a/b处的节点

    [题目]: 给定链表的头节点 head,实现删除链表的中间节点的函数. 例如: 步删除任何节点: 1->2,删除节点1: 1->2->3,删除节点2: 1->2->3-& ...

  8. 剑指offer——19删除链表的节点

    题目一: 在O(1)时间内删除链表节点.给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间内删除该节点. 书本讲得不明就里 class Solution { void DeleteNode ...

  9. LeetCode 面试题18. 删除链表的节点

    题目链接:https://leetcode-cn.com/problems/shan-chu-lian-biao-de-jie-dian-lcof/ 给定单向链表的头指针和一个要删除的节点的值,定义一 ...

随机推荐

  1. 第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)

    Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第i位开始匹配2^k个 ...

  2. DTV_SI 汇总 & 兼谈LCN

    前言 本章主要对数字广播DVB做一个系统的概况的描述,以及一些spc的相关的内容,虽然流程分析的不多,但是做为后续 章节资料的源泉,也是不可或缺的. 一. ATSC和DVB数字电视系统的比较 本文的主 ...

  3. C的结构体函数

    #include<stdio.h> #include<string.h> struct Test { int age; ]; double score; }std1; //结构 ...

  4. maven中的三种工程,以及在idea中构建父子工程。

    1.pom工程:用在父级工程或聚合工程中.用来做jar包的版本控制.主要是定义POM文件,将后续各个子模块公用的jar包等统一提出来,类似一个抽象父类 2.war工程:将会打包成war,发布在服务器上 ...

  5. recast 生成navmesh主要流程

    参考:      critterai  http://www.critterai.org      recast & Detour https://github.com/recastnavig ...

  6. bzoj 4300: 绝世好题【dp】

    设f[i][j]表示数列到i为止最后一项第j位为1的最大子序列长度,每次从i-1中1<<j&a[i]!=0的位+1转移来 然后i维是不需要的,答案直接在dp过程中去max即可 #i ...

  7. 回调函数,回调函数使用call

    回调函数:一个函数b作为参数,给另外一个函数a使用.并且在执行a之后(注意不一定是执行完a),再去执行b这个函数. 上代码: function a(callback) { alert("我是 ...

  8. JavaScript--userAgent

    userAgent 返回用户代理头的字符串表示(就是包括浏览器版本信息等的字符串) 语法 navigator.userAgent 几种浏览的user_agent.,像360的兼容模式用的是IE.极速模 ...

  9. Hdu 3487 play the chain

    Description 瑶瑶很喜欢玩项链,她有一根项链上面有很多宝石,宝石从1到n编号. 首先,项链上的宝石的编号组成一个序列:1,2,3,...,n. 她喜欢两种操作: 1.CUT a b c:他会 ...

  10. hdu1166 敌兵布阵(树状数组)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...