zoukankan      html  css  js  c++  java
  • 链表问题

    找到链表倒数第k个节点

    class Solution {
        public ListNode getKthFromEnd(ListNode head, int k) {
    if(head == null) return null;
    ListNode dummyHead = new ListNode(-1);
    dummyHead.next = head;
    ListNode fast = dummyHead;
    ListNode slow = dummyHead;
    for(int i = 0 ; i < k;i ++){
        fast = fast.next;
    }
    while(fast!=null){
        fast = fast.next;
        slow = slow.next;
    }
    return slow;
        }
    }

    删除链表倒数第n个结点

    class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode dummyHead = new ListNode(-1);
            dummyHead.next=head;
            ListNode  fast = dummyHead;
            ListNode  slow = dummyHead;
            for(int i=0;i<n+1;i++){//快指针先移动N+1个位置
                fast=fast.next;
             }
            while(fast!=null){//快指针指向最后一个节点停下 此时慢指针指向倒数第N+1个结点
                slow=slow.next;
                fast=fast.next;       
             }
            slow.next=slow.next.next;//删除倒数第N个结点
            return dummyHead.next;
        }
    }

    合成两个有序链表

    class Solution {
        public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    if(l1 == null) return l2;
    if(l2 == null) return l1;
    if(l1.val < l2.val){
        l1.next = mergeTwoLists(l1.next,l2);
        return l1;
    }else{
        l2.next = mergeTwoLists(l1,l2.next);
        return l2;
    }
        }
    }

    合成K个有序链表

    class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
            if (lists == null || lists.length == 0) {
                return null;
            }
            return merge(lists, 0, lists.length - 1);
        }
    
        private ListNode merge(ListNode[] lists, int left, int right) {
            // 拆分直到剩下一个链表直接返回。
            if (left == right) {
                return lists[left];
            }
            // 找到数组的中间位置,并不断递归合并左右两部分为一个有序链表。
            int mid = left + (right - left) / 2;
            ListNode l1 = merge(lists, left, mid);
            ListNode l2 = merge(lists, mid + 1, right);
            return mergeTwoLists(l1, l2);
        }
    
        private ListNode mergeTwoLists(ListNode l1, ListNode l2) {
            // 一个链表合并完则直接返回另一个链表。
            if (l1 == null) {
                return l2;
            }
            if (l2 == null) {
                return l1;
            }
            // 递归合并两个有序链表,优先拼接两个链表中的较小节点。
            if (l1.val < l2.val) {
                l1.next = mergeTwoLists(l1.next, l2);
                return l1;
            }
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }
    }

    链表相加

    class Solution {
        public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode dummy = new ListNode(0);
    int carry = 0;
    ListNode cur = dummy;
    
    
    while(l1 != null || l2 != null){
        int val1 = l1 == null ? 0 : l1.val;
        int val2 = l2 == null ? 0 : l2.val;
        int sum = val1 + val2+carry;
        int val = sum % 10;
        carry = sum / 10;
     cur.next = new ListNode(val);
     cur = cur.next;
    
     if(l1 != null)
     l1 = l1.next;
     if(l2 != null)
     l2 = l2.next;
    }
    if(carry == 1){
        cur.next = new ListNode(1);
    }
    return dummy.next;
        }
    }

    两两交换链表中的结点

    class Solution {
        public ListNode swapPairs(ListNode head) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode pre = dummy;
    while(pre.next != null && pre.next.next != null){
    ListNode start = pre.next;
    ListNode end = pre.next.next;
    start.next = end.next;
    end.next = start;
    pre.next = end;
    pre = start;
    }
    return dummy.next;
        }
    }

    K个一组反转链表

    class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
               
                   ListNode dummy = new ListNode(0);
                   dummy.next = head;
                   ListNode pre = dummy;
                   ListNode end = dummy;
                   while(end.next != null){
                   for(int i = 0; i < k&&end != null; i ++){
                       end = end.next;
                      }
                   if(end == null){
                       break;
                   }
                 ListNode start = pre.next;
                 ListNode next = end.next;
                 end.next = null;
                 pre.next = reverse(start);
                start.next = next;
                pre = start;
    
               end = pre;
    
    
    
    
    
               }
               return dummy.next;
    
        }
    
    
    
        private ListNode reverse(ListNode head){
            ListNode cur = head;
            ListNode pre = null;
            while(cur != null){
                ListNode next = cur.next;
                cur.next = pre;
                pre = cur;
                cur = next;
            }
            return pre;
        }
    }
  • 相关阅读:
    git常用命令总结
    chrome浏览器使用技巧
    chorme浏览器的Access-Control-Allow-Origin拦截限制
    基于HP DL388 Gen 9服务器基本配置(ESXI 6.5)
    c# winform 窗体之间的传参
    <<测试驱动开发的艺术>>读书笔记
    <<人性的弱点>>读书笔记
    一个很奇怪的重复链接lib的问题
    redis集群的一些笔记
    <<敏捷开发>>读书笔记
  • 原文地址:https://www.cnblogs.com/moonyaoo/p/12844625.html
Copyright © 2011-2022 走看看