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/ 给定单向链表的头指针和一个要删除的节点的值,定义一 ...
随机推荐
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- UVa10288 Coupons 分数模版
UVa10288 题目非常简单, 答案就是 n/n+n/(n-1)+...+n/1; 要求分数输出.套用分数模板.. #include <cstdio> #include <cstr ...
- 杂项-公司:Sun
ylbtech-杂项-公司:Sun Sun Microsystems是IT及互联网技术服务公司(已被甲骨文收购)Sun Microsystems 创建于1982年.主要产品是工作站及服务器.1986年 ...
- EasyUI Validatebox 验证框
转自:http://www.jeasyui.net/plugins/167.html 通过 $.fn.validatebox.defaults 重写默认的 defaults. 验证框(validate ...
- 6章 Models
传统的MVC结构中,有模型这么一个概念.Django中,Models又是怎么一回事呢? 刚才生成的这些乱七八糟的数据迁移就是Django自带的一些应用 INSTALLED_APPS = [ 'djan ...
- android_app c++框架
找遍了全网,没有一个完整的可用的框架.ndk自带的android_native_app_glue确实不太好用,闭关几天,写出了一个框架.完全的消息队列调用,目前测试的主体框架是没有什么问题了,程序入口 ...
- List 的属性与方法整理
List<T> 类与 ArrayList 类比较类似.它实现了 IList<T> 泛型接口,长度可以动态增加. 可以使用 Add 或 AddRange 方法将项添加到 List ...
- SQL 几个查看性能的语句
1.查找目前SQL Server所执行的SQL语法,并展示资源情况: SELECT s2.dbid , DB_NAME(s2.dbid) AS [数据库名] , --s1.sql_handle , ( ...
- Python,计算 ax^2 + bx + c = 0的根
1 #-*-coding : utf-8-*- 2 import math 3 4 def quadratic(a, b, c): 5 if not isinstance(a, (int, float ...
- opencv3.31+vs2015终于配置成功了
风萧萧兮易水寒, 熬了几个夜晚,终于把opencv配好了, 来图一 唉试了很多方法,终于成功. 教程和资料会发在个人网站里. 测试 代码 #include <iostream> #incl ...