赞
踩
题比较简单,注意一些细节,比如pre的重定位
- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode() {}
- * ListNode(int val) { this.val = val; }
- * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
- * }
- */
- class Solution {
- public ListNode removeElements(ListNode head, int val) {
-
- //定义“指针”
- ListNode dummyhead = new ListNode(-1,head);
- ListNode pre = dummyhead;
- ListNode cur = head;
-
- while(cur != null) {
- //两种情况pre都要走
- if( val == cur.val) {
- pre.next = cur.next;
- } else {
- pre = cur;
- }
- cur = cur.next;
- }
-
- return dummyhead.next;
-
-
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。