【链表】- 删除链表的中间节点和a/b处的节点
2017-12-07 11:45
357 查看
【题目】
给定链表的头节点head,实现删除链表的中间节点的函数
给定链表的头节点head,整数a和b,实现删除位于a/b处节点的函数
//删除链表的中间节点
public class Node{
public int value;
public Node next;
public Node(int data){
this.value = data;
}
}
public Node removeMidNode(Node head){
if(head == null || head.next == null){
return head;
}
if(head.next.next == null){
return head.next;
}
Node pre = head;
Node cur = head.next.next;
while(cur.next != null && cur.next.next != null){
pre = pre.next;
cur = cur.next.next;
}
pre.next = pre.next.next;
return head;
}
给定链表的头节点head,实现删除链表的中间节点的函数
给定链表的头节点head,整数a和b,实现删除位于a/b处节点的函数
//删除链表的中间节点
public class Node{
public int value;
public Node next;
public Node(int data){
this.value = data;
}
}
public Node removeMidNode(Node head){
if(head == null || head.next == null){
return head;
}
if(head.next.next == null){
return head.next;
}
Node pre = head;
Node cur = head.next.next;
while(cur.next != null && cur.next.next != null){
pre = pre.next;
cur = cur.next.next;
}
pre.next = pre.next.next;
return head;
}
//删除链表的a/b处的节点 public Node removeByration(Node head, int a, int b){ if(a < 1 || a > b){ return head; } int n = 0; Node cur = head; while(cur != null){ n++; cur = cur.next; } n = (int)Math.ceil(((doule)(a*n))/(double)b); if(n==1){ head = head.next; } if(n>1){ cur = head; while(--n != 1){ cur = cur.next; } cur.next = cur.next.next; } return head; }
相关文章推荐
- (转)问题:假设一个没有头指针的单链表。一个指针指向此单链表中间的一个节点(既不是第一个,也不是最后一个节点),请将该节点从单链表中删除。
- [算法]删除链表的中间节点
- 删除单链表的头元素;单链表,只是遍历一次,求出中间节点
- 删除链表中的中间节点和 a/b 处的结点 Python 版
- 关于长单链表中间节点删除
- 左神的书——《程序员代码面试指南》之删除链表的中间节点或a/b节点 c++实现
- 2.3-删除链表中间节点
- 链表问题 删除链表的中间节点和a/b处的节点
- 删除链表的中间节点
- 链表问题---删除链表的中间节点和a/b处的节点
- (转)问题:假设一个没有头指针的单链表。一个指针指向此单链表中间的一个节点(既不是第一个,也不是最后一个节点),请将该节点从单链表中删除。
- 删除链表的中间节点和a/b处的节点
- 删除链表的中间节点 / 删除链表a/b处的节点
- 左程云_算法与数据结构 — 链表问题 — 03删除链表的中间节点和a/b处的节点
- 单链表删除头节点和不知道N的情况下寻找中间节点
- 给定链表中间节点指针,删除中间节点的方法
- 9.2链表(三)——删除单向链表中间的某个节点,假定你只能访问该节点
- 删除链表中一个位于中间位置的节点
- C++删除链表中间节点的方法
- 链表面试题逆序打印,删除无头非尾节点,非头位置插入,约瑟夫环,查找中间和倒数第k个节点