Lettcode_203_Remove Linked List Elements

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/45868027

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

思路:

(1)题意为给定一个链表和一个整数,从该链表中删除所有值为该整数的节点。

(2)该题主要考察链表的操作。首先,对于类似a->a->a->b->c....这样需要移除a的链表进行判断,得到第一个和目标值不相同的节点,设定指针r指向该节点,这个节点即为结果链表的起始节点;其次,设定指针s和t分别指向当前节点和当前节点的下一个节点,其中s用来标记待删除节点后面的节点,t用来标记待判断需删除的节点。这样,对链表进行遍历,如果t指向节点的值和目标值相同,则t指向其所指节点的下一个节点,s的下一个节点指向t;如果t指向节点的值和目标值不相同,则s和t分别后移;最后,遍历完整个链表,所得r即为结果链表。

(3)详情见下方代码。希望本文对你有所帮助。

算法代码实现:

/**
	 * @author liqqc
	 * @param head
	 * @param val
	 */
	public static ListNode removeElements(ListNode head, int val) {
		if (head == null)
			return null;

		ListNode r = head;
		ListNode s = head;
		ListNode t = head.next;
		// 起始位置的确定
		while (s != null && s.val == val) {
			s = s.next;
			if (t != null) {
				t = t.next;
			}
			r = s;
		}

		while (t != null) {
			// 如果相同则移除
			if (t.val == val) {
				t = t.next;
				s.next = t;
			} else {
				s = s.next;
				t = t.next;
			}
		}
		return r;
	}
时间: 2024-08-28 16:13:17

Lettcode_203_Remove Linked List Elements的相关文章

leetcode-203-Remove Linked List Elements

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 Credits: Special thanks to @m

LeetCode Remove Linked List Elements 删除链表元素

题意:移除链表中元素值为val的全部元素. 思路:算法复杂度肯定是O(n),那么就在追求更少代码和更少额外操作.我做不出来. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 1

leetCode 203. Remove Linked List Elements 链表

203. Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 题目大意: 删除链表中全部的目标元素. 代码如下:

[LeetCode][JavaScript]Remove Linked List Elements

Remove Linked List Elements Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 https://leetcode.com/problems/r

leetcode_203题——Remove Linked List Elements(链表)

Remove Linked List Elements Total Accepted: 8053 Total Submissions: 29898My Submissions Question Solution Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val =

【LeetCode-面试算法经典-Java实现】【203-Remove Linked List Elements(删除单链表中的元素)】

[203-Remove Linked List Elements(删除单链表中的元素)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 代码下载[https://github.com/Wang-Jun-Chao] 原题 Remove all elements from a linked list of integers that have value val. Example Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5

leetcode Remove Linked List Elements 203

Remove Linked List Elements Total Accepted: 11291 Total Submissions: 42824 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

203. Remove Linked List Elements - LeetCode

Question 203.?Remove Linked List Elements Solution 题目大意:从链表中删除给定的数 思路:遍历链表,如果该节点的值等于给的数就删除该节点,注意首节点 Java实现: public ListNode removeElements(ListNode head, int val) { ListNode cur = head; while (cur != null) { if (cur.next != null && cur.next.val ==

203. Remove Linked List Elements

Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5 1 /** 2 * Definition for singly-linked list. 3 * struct Lis