您当前的位置: 首页 > 
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

查询节点并删除

沙漠一只雕得儿得儿 发布时间:2016-11-15 16:33:05 ,浏览量:0

package 链表中;

import com.jikexueyuan.listNode.ListNode;

/**
 * 查询节点并删除
 */
public class RemoveLinkedListElements {
	public ListNode removeElements(ListNode head, int val) {
		if (head == null) {
			return head;
		} else {
			ListNode newHead = new ListNode(0);
			newHead.next = head;
			ListNode pre = newHead;
			ListNode p = head;
			while (p != null) {
				if (p.val == val) {
					pre.next = p.next;
					p = p.next;
				} else {
					pre = p;
					p = p.next;
				}
			}
			return newHead.next;
		}
	}
	@Test
	public void test() {
		int[] array1 = {1, 6, 6, 4, 6, 3, 5};
		ListNode head = ListNode.arrayToList(array1);
		head = removeElements(head, 6);
		ListNode.printList(head);
		int[] array2 = {1, 2, 3, 4, 1};
		head = ListNode.arrayToList(array2);
		head = removeElements(head, 1);
		ListNode.printList(head);
	}
}
关注
打赏
1657159701
查看更多评论
立即登录/注册

微信扫码登录

0.0354s