LeetCode【203.移除链表的元素】

版权声明: https://blog.csdn.net/qq_38386316/article/details/82960169

题目描述

删除链表中等于给定值val的所有节点。

示例

输入 1 > 2 > 6 > 3 > 4 > 5 > 6 , v a l = 6 1->2->6->3->4->5->6, val = 6
输出 1 > 2 > 3 > 4 > 5 1->2->3->4->5

思路 * 1
申请一个头结点cur,并使用一个变量dummpy指向其初始的位置,curhead同时向前遍历,head总比cur要快一个结点。当head中结点的值达到要求时(即head.val = val),使较慢的cur.next直接指向head的下一个结点(当然也可以是null),此时cur不用继续向前,head向前一步后,通过达到的结点后,cur继续向前。最终返回dummpy.next即可。

代码 * 1

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode cur = new ListNode(-1);
        ListNode dummpy = cur;
        while(head != null) {
            if(head.val == val) {
                cur.next = head.next;
            }else { 
                cur.next = head;
                cur = cur.next;
            }
            head = head.next;
        }
        return dummpy.next;
    }
}

复杂度分析

  • 时间复杂度: O N O(N)
  • 空间复杂度: O 1 O(1)

思路 * 2
还是可以将上面的思路改成递归的思路,很是简便的代码量(但是数据量过大时,会造成栈的溢出)。

代码 * 2

 if (head == null) return null;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;

复杂度分析

  • 时间复杂度: O N O(N)
  • 空间复杂度: O N O(N)

完整代码

package leetcode203;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by 张超帅 on 2018/10/7.
 */
class ListNode {
    int val;
    ListNode next;
    ListNode( int val) {this.val = val;}
}
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        /*ListNode cur = new ListNode(-1);
        ListNode dummpy = cur;
        while(head != null) {
            if(head.val == val) {
                cur.next = head.next;
            }else {
                cur.next = head;
                cur = cur.next;
            }
            head = head.next;
        }
        return dummpy.next;
    */
        if (head == null) return null;
        head.next = removeElements(head.next, val);
        return head.val == val ? head.next : head;
    }
}
public class leetcode203 {
    public static int[] stringToArrays(String input) {
        input = input.trim();
        input = input.substring(1,input.length() - 1);
        if(input == null){
            return new int[0];
        }
        String [] parts = input.split(",");
        int[] res = new int[parts.length];
        for(int i = 0; i < parts.length; i ++) {
            res[i] = Integer.parseInt(parts[i].trim());
        }

        return res;
    }
    public static ListNode stringToListNode(String input) {
        int[] nodes = stringToArrays(input);
        ListNode cur = new ListNode(-1);
        ListNode dummpy = cur; // 不能写成 ListNode dummpy = cur.next;
        for(int node : nodes) {
            cur.next = new ListNode(node);
            cur = cur.next;
        }
        System.out.println(dummpy.next.val);
        return dummpy.next;
    }
    public static String listnodeTostring(ListNode head) {
        if(head == null) return "[]";
        String res = "";
        while(head != null) {
            res += head.val + ", ";
            head = head.next;
        }
        return "[" + res.substring(0, res.length() - 2) + "]";
    }
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
        String line ;
        while((line = in.readLine()) != null) {
            ListNode head = stringToListNode(line);
            int num = Integer.parseInt(in.readLine());
            ListNode ret = new Solution().removeElements(head, num);
            String result = listnodeTostring(ret);
            System.out.println(result);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_38386316/article/details/82960169