Java链表操作-删除链表中指定元素,删除指定下标元素,反转链表

  • 删除链表中指定元素
  • 删除指定下标的节点
  • 添加节点
  • 反转链表
  • 输出链表
//定义链表
class ListNode {
    int val;
    ListNode next;

    public ListNode(int x) {
        val = x;
    }
}

public class Solution {
    //删除链表中指定的元素
    public static ListNode removeElements(ListNode head, int val) {
        while (head != null && head.val == val) {
            head = head.next;
        }
        if (head == null) {
            return null;
        }
        ListNode prev = head;
        while (prev.next != null) {
            if (prev.next.val == val) {
                prev.next = prev.next.next;
            } else {
                prev = prev.next;
            }
        }
        return head;
    }
    //添加元素
    public static ListNode addNode(ListNode head, int val) {
        ListNode newNode = new ListNode(val);
        if (head == null) {
            head = newNode;
            return head;
        }
        ListNode p = head;
        while (p.next != null) {
            p = p.next;
        }
        p.next = newNode;
        return head;
    }
    //打印链表
    public static void printListNode(ListNode head) {
        StringBuffer stringBuffer = new StringBuffer();
        if (head != null) {
            stringBuffer.append("{");
            ListNode p = head;
            while (p != null) {
                stringBuffer.append(p.val + ",");
                p = p.next;
            }
            stringBuffer.replace(stringBuffer.length() - 1, stringBuffer.length(), "}");
        }
        System.out.println(stringBuffer.toString());
    }

    //删除指定位置的节点
    public static ListNode removeIndex(ListNode head, int index) {
        if (index < 1 || index > length(head)) {
            return head;
        }
        if (index == 1) {
            head = head.next;
            return head;
        }
        int i = 1;
        ListNode p = head;
        while (p.next != null) {
            if (i != index) {
                p = p.next;
                i++;
            } else {
                p.next = p.next.next;
            }
        }
        return head;
    }

    //反转链表
    public static ListNode reverseListNode(ListNode head) {
        if (head == null) return null;
        ListNode reverseNode = null;
        if (head.next == null){
            reverseNode = head;
            return reverseNode;
        }

        ListNode next = null;
        while (head != null) {
            next = head.next;
            head.next = reverseNode;
            reverseNode = head;
            head = next;
        }
        return reverseNode;
    }

	//求链表长度
    public static int length(ListNode head) {
        int length = 0;
        ListNode tmp = head;
        while (tmp != null) {
            length++;
            tmp = tmp.next;
        }
        return length;
    }

	//测试
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String sin = scanner.nextLine();
        String[] strings = sin.split(",");
        List<String> linkList = new ArrayList<>();
        List<String> tarNums = new ArrayList();
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].contains("[")) {
                strings[i] = String.valueOf(strings[i].charAt(1));
            }
            if (strings[i].contains("]")) {
                strings[i] = String.valueOf(strings[i].charAt(0));
                tarNums = Arrays.asList(Arrays.copyOfRange(strings, i + 1, strings.length));
                linkList = Arrays.asList(Arrays.copyOfRange(strings, 0, i));
            }
        }
        ListNode result = null;
        ListNode head = null;
        for (String s : linkList) {
            head = addNode(head, Integer.valueOf(s));
        }
        for (String val : tarNums) {
            result = removeElements(head, Integer.valueOf(val));
        }
        printListNode(result);
    }
}    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值