zoukankan      html  css  js  c++  java
  • 11/4 <LinkedList>

    328. Odd Even Linked List

    “穿针引线”

    1. odd指针穿奇数位

    2.even指针穿偶数位

    3.用evenHead记录第一个偶数位节点,最后用odd连接evenHead

    注意判断空节点以及一个节点的情况。

    class Solution {
        public ListNode oddEvenList(ListNode head) {
            if(head == null || head.next == null)
                return head;
            
            ListNode odd = head, even = head.next, evenHead = even;
            
            while(even != null && even.next != null){
                odd.next = even.next;
                odd = odd.next;
                even.next = odd.next;
                even = even.next;
            }
            odd.next = evenHead;
            return head;
        }
    }

    92. Reverse Linked List II

    1. 对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点。

    每一步将cur后面的节点设置为temp节点,目的是为了把temp节点交换到pre.next的位置。这样的过程需要保持n-m次。

    class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode pre = dummy;
            ListNode cur = head;
            
            if(head == null || head.next == null)
                return head;
    
            for(int i = 1; i < m; i++){
                pre = pre.next;
                cur = pre.next;
            }
              
            
            for(int i = m; i < n; i++){
                ListNode temp = cur.next;
                cur.next = temp.next;
                temp.next = pre.next;
                pre.next = temp;
            }
            return dummy.next;
        }
    }
  • 相关阅读:
    宏观经济指标
    Poloniex API 文档
    雪球释老毛推荐的投资者书单
    数字货币开源项目——貔貅
    数字货币量化分析报告_20170905_P
    数字货币量化分析报告_2017-09-05
    数字货币量化分析报告_2017-09-04
    ICO成本价
    Python3使用Print输出带颜色字体
    Ta-lib函数功能列表
  • 原文地址:https://www.cnblogs.com/Afei-1123/p/11791445.html
Copyright © 2011-2022 走看看