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. python cmd 启动python项目报错:no module named “xxx”

    场景:使用pycharm编辑器启动pyhon项目时可以启动,但使用cmd启动时,会报:no module named “xxx”的错误,此时,有两种情况: 1.no module named “xxx ...

  2. LaTex简历排版

    为了写好简历,倒腾了好几天的Latex,主要是因为中文支持问题还有morderncv的更新问题... 1.  http://www.ctex.org/CTeXDownload/ 建议下载v2.9.2. ...

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

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

  4. 洛谷P4887 第十四分块(前体)(二次离线莫队)

    题面 传送门 题解 lxl大毒瘤 我们考虑莫队,在移动端点的时候相当于我们需要快速计算一个区间内和当前数字异或和中\(1\)的个数为\(k\)的数有几个,而这个显然是可以差分的,也就是\([l,r]\ ...

  5. 【js】为什么要使用react+redux

    前端的浪潮一叠叠袭来,带走了jQuery,带走了backbone,带来了react,带来了redux,但是面对层出不穷的前端技术,我们应该何去何从呢?近一年来笔者的也发生了同样的变化,技术栈从.net ...

  6. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

  7. HTML基础2——综合案例2——复杂的嵌套列表

    <html> <head> <title></title> </head> <body> <ul type="d ...

  8. python获取主机名和用户名

    import socketimport getpassuser_name = getpass.getuser() # 获取当前用户名hostname = socket.gethostname() # ...

  9. 查询编辑器便捷特性【MSSQL】

    SQL Server团队为用户提供了一个便捷的特性 如果没有突出显示文本,那么按F5,执行整个批处理. 如果突出显示文本(选中SQL命令),那么只执行选中文本.

  10. 个人作业-Alpha测试

    课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/ 作业要求 https://edu.cnblogs.com/campu ...