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/ 给定单向链表的头指针和一个要删除的节点的值,定义一 ...
随机推荐
- codeforces round 416 div2 补题 CF 811 A B C D E
A. Vladik and Courtesy 水题略过 #include<cstdio> #include<cstdlib> #include<cmath> usi ...
- 29. ExtJs - Struts2 整合(1) - 登录页面
转自:https://yarafa.iteye.com/blog/729197 初学 ExtJS,在此记录下学习过程中的点点滴滴,以备不时只需,也希望能给跟我一样的菜鸟一些帮助,老鸟请忽略.如有不当之 ...
- goalng——time包学习
1.星期:type Weekday int const ( Sunday Weekday = iota Monday Tuesday Wednesday Thursday Friday Saturda ...
- 【第三届强网杯】write up
一,鲲or鳗orGame 从浏览器里拿到game.gb 用 VisualBoyAdvance ,搜索内存变量,然后改变量就完事了,但是改变量他们说直接利用添加代码功能有bug,所以要在工具里的内存查看 ...
- C#常量知识整理
整数常量 整数常量可以是十进制.八进制或十六进制的常量.前缀指定基数:0x 或 0X 表示十六进制,0 表示八进制,没有前缀则表示十进制. 整数常量也可以有后缀,可以是 U 和 L 的组合,其中,U ...
- LPS HDOJ 4745 Two Rabbits
题目传送门 /* 题意:一只兔子顺时针跳,另一只逆时针跳,跳石头权值相等而且不能越过起点 LPS:这道就是LPS的应用,把环倍增成链,套一下LPS,然而并不能理解dp[i][i+n-2] + 1,看别 ...
- 笔试题的各种trick
%x 默认去掉前导零 #include<stdint.h> #include<stdio.h> union X { int32_t a; struct ...
- 聊聊MyBatis缓存机制
https://tech.meituan.com/mybatis_cache.html 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用MyBatis的默认 ...
- Python的I/O操作
1.读取键盘输入 msg = raw_input("Please enter :") print "you input ",msg #可接受Python表达式作 ...
- exe4j将可执行的jar封装成exe文件
1,将java项目打包成可执行的jar:https://www.cnblogs.com/3b2414/p/9355292.html, 2,下载好exe4j工具, 3,首先注册,如果你不注册,打包好的软 ...