lintcode:删除链表中指定元素
生活随笔
收集整理的这篇文章主要介绍了
lintcode:删除链表中指定元素
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
题目
删除链表中等于给定值val的所有节点。
样例给出链表 1->2->3->3->4->5->3, 和 val = 3, 你需要返回删除3之后的链表:1->2->4->5。
解题
加入头结点进行删除
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/ public class Solution {/*** @param head a ListNode* @param val an integer* @return a ListNode*/public ListNode removeElements(ListNode head, int val) {// Write your code hereif(head == null)return head;ListNode dummy = new ListNode(0);dummy.next = head;head = dummy;while (head.next != null) {if (head.next.val == val) {head.next = head.next.next;} else {head = head.next;}}return dummy.next;} }
总结
以上是生活随笔为你收集整理的lintcode:删除链表中指定元素的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 前端框架Bootstrap 教程
- 下一篇: android查看Logcat