比较经典的几道链表相关算法题

合并K个链表

思路:使用容量为K的最小堆优先队列,把链表的头节点都放进去;然后出队当前优先队列中最小的,放入链表;再判断出队的那个节点是否有下一个节点,有,就将出队那个节点的下一个入队,再出队当前优先队列中的最小的,直到优先队列为空。

Class Main {
    public ListNode mergeKLists(ListNode[] lists) {
        if(lists.length == 0) return null;
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        //创建一个容量为K的最小堆优先队列
        PriorityQueue<ListNode> q = new PriorityQueue<>(new Comparator<ListNode>(){
           @Override
            public int compare(ListNode o1, ListNode o2) {
                return o1.val - o2.val;//升序排列
            }
        });
        for(ListNode list : lists){
            if(list != null){
                q.offer(list);//将每条链表的头节点放入最小堆优先队列
            }
        }
        while(!q.isEmpty()) {
            ListNode node = q.poll();
            cur.next = node;
            cur = cur.next;
            if(node.next != null) {
                q.offer(node.next);
            }
        }
        return dummy.next;
    }
}
public class ListNode{
    int val;
    ListNode next;
    ListNode(){}
    ListNodde(int val){
        this val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

合并两个有序链表

迭代法:首先当两条链表(l1,l2)都不是空链表时,判断哪一条链表的头节点值更小,将较小的节点的值添加到结果里,当一个节点被添加到结果里之后,将对应链表中的节点向后移动一位。

class Main{
    public ListNode meergeTwoLists(ListNode l1, ListNode L2) {
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while(l1 != null && l2 != null) {
            if(l1.val < l2.val) {
                cur.next = l1;
                cur = cur.next;
                l1 =l1.next;
            }else {
                cur.next = l2;
                cur = cur.next;
                l2 = l2.next;
            }
        }
        if(l1 == null) cur.next = l2;
        if(l2 == null) cur.next = l1;
        return dummy.next;
    }
}
public class ListNode{
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this. next = next;
    }
}

链表倒数第K个节点

class Main {
    public ListNode getKthFromEnd(ListNode head, int k) {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(k-- > 0) {
            fast = fast.next;
        }
        while(fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
}

删除链表倒数第K个节点

class Main {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null) return null;
        ListNode fast = head;
        ListNode slow = head;
        while(n-- > 0) {
            fast = fast.next;
        }
        //如果n的值刚好是链表的长度,直接返回去掉头节点之后的链表
        if(fast == null) return head.next;
        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return head;
    }
}
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

删除链表中的重复元素

1->2->3->3->4->4->5 ==> 1->2->5

由于给定的链表是排好序的,因此重复的元素在链表中出现的位置是连续的,因此我们只需要对链表进行一次遍历,就可以删除重复的元素。由于链表的头节点可能会被删除,因此我们需要额外使用一个哑节点来指向链表的头节点。

class Main {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        
        ListNode pre = dummy;
        ListNode cur = head;
        while(cur != null && cur.next != null) {
            if(cur.val == cur.next.val){
                ListNode temp = cur.next;
                while(temp != null && temp.val == cur.val) {
                    temp = temp.next;
                }
                pre.next = temp;
                cur = temp;
            }else {
                pre = pre.next;
                cur = cur.next;
            }
        }
        return dummy.next;
    }
}
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

奇偶链表

class Main {
    public ListeNode oddEvenList(ListeNode head) {
        if(head == null) return head;
        if(head.next == null) return head;
        ListNode odd = head;
        ListNode evenHead = head.next;
        ListNode even = evenHead;
        while(even != null && even.next != null) {
            odd.next = odd.next.next;
            odd = odd.next;
            even.next = even.next.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
public class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) {
        this.val = val;
    }
    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

两两交换链表中的节点

给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
迭代

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode pre = dummy;
        while(pre.next != null && pre.next.next != null) {
            ListNode t = pre.next.next;
            pre.next.next = t.next;
            t.next = pre.next;
            pre.next = t;
            pre = t.next;
        }
    return dummy.next;
    }
}

递归

class Solution {
    public ListNode swapPairs(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode first = head;
        ListNode second = head.next;
        head = second;
        first.next = swapPairs(second.next);
        second.next = first;
         return second;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值