List-remove element
来源:互联网 发布:python arff 读取 编辑:程序博客网 时间:2024/06/05 15:05
237. 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 that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4
and you are given the third node with value 3
, the linked list should become 1 -> 2 -> 4
after calling your function.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public void deleteNode(ListNode node) { node.val = node.next.val; node.next = node.next.next; }}
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.
Example
Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6
Return: 1 --> 2 --> 3 --> 4 --> 5
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */class Solution { public ListNode removeElements(ListNode head, int val) { ListNode dummpy = new ListNode(0); dummpy.next = head; head = dummpy; while(head.next!=null){ if(head.next.val == val){ head.next = head.next.next; }else{ head = head.next; } } return dummpy.next; }}
82. Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *///因为可能原先的头结点也存在重复,所以头结点不确定,这时候可以考虑使用dummpy Nodeclass Solution { public ListNode deleteDuplicates(ListNode head) { ListNode dummpy = new ListNode(0); dummpy.next = head; head = dummpy; while(head.next!=null && head.next.next!=null){ if(head.next.val == head.next.next.val){ //删掉所有重复的元素 int val = head.next.val; while(head.next != null && head.next.val == val){ head.next = head.next.next; } }else{ //往后移动一位 head = head.next; } } return dummpy.next; }}
阅读全文
0 0
- List-remove element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- Remove Element
- leetcode 33|81. Search in Rotated Sorted Array 1|2
- 使用STLINK下载程序到STM8S103问题汇总
- PHP 导出csv文件 首位是0 无法导出
- eclipse开发项目在正常模式下起服务正常,可是debug模式下特别的慢,解决办法
- linux 交叉编译 tcpdump以及tcpdump的使用
- List-remove element
- ${pageContext.request.contextPath} JSP取得绝对路径
- 详解C语言中strpbrk()函数的用法
- python创建数据库
- Linux 树莓派上简单的网络编程程序实现
- Linux TCP/IP 协议栈调优
- final关键字可以用来修饰类、方法、变量、参数,不能修饰抽象类和接口
- webpack--入门篇
- js url正则解析