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 教程
- 下一篇: C++的iostream标准库介绍+使用