【数据结构】链表的基本应用1

1.移除链表元素
(删除头结点时另做考虑)

public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return head;
        }
        ListNode rs = new ListNode(0);
        rs.next = head;
        ListNode cur = rs;
        while(cur.next != null){
            if(cur.next.val == val){
                cur.next = cur.next.next;
            }else{
                cur = cur.next;
            }
        }
        return rs.next;
    }

(添加一个虚拟头结点)

public ListNode removeElements2(ListNode head, int val) {
       ListNode pre = new ListNode(0);
        pre.next = head;
        ListNode cur = pre;
        while(cur.next != null) {
            if(cur.next.val == val) {
                cur.next = cur.next.next;
            }else {
                cur = cur.next;
            }
        }
        return pre.next;
    }

2.反转链表
(迭代)

public ListNode reverseList1(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while(cur != null) {
            ListNode temp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

(递归)

public ListNode reverseList2(ListNode head) {
       if(head == null || head.next == null) {
           return head;
       }
        ListNode p = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return p;
    }

3.链表的中间结点
( 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点)
快慢指针法

public ListNode middleNode1(ListNode head) {
        if(head == null || head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

依次遍历

public ListNode middleNode2(ListNode head) {
       int count = 0;
        ListNode list = head;
        while(list != null){
            count++;
            list = list.next;
        }
        int t = count / 2;
        while(head != null && t > 0){
            head = head.next;
            t--;
        }
        return head;
    }

4. 输入一个链表,输出该链表中倒数第n个结点(快慢指针法)

 public ListNode removeNthFromEnd(ListNode head, int n) {
        
        ListNode cur = new ListNode(0);
        cur.next = head;
        ListNode fast = cur;
        ListNode slow = cur;
        for(int i = 0;i < n;i++) {
            fast = fast.next;
        }
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return cur.next;
    }

5.合并两个有序链表

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        
         if (l1 == null) {
            return l2;
        }

        if (l2 == null) {
            return l1;
        }

        ListNode result = null;
        ListNode last = null;

        ListNode cur1 = l1;
        ListNode cur2 = l2;

        while (cur1 != null && cur2 != null) {
            if (cur1.val <= cur2.val) {
                if (result == null) {
                    result = cur1;
                } else {
                    last.next = cur1;
                }
                last = cur1;
                cur1 = cur1.next;
            } else {
                if (result == null) {
                    result = cur2;
                } else {
                    last.next = cur2;
                }
                last = cur2;
                cur2 = cur2.next;
            }
        }

        if (cur1 != null) {
            last.next = cur1;
        } else {
            last.next = cur2;
        }

        return result;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值