zoukankan      html  css  js  c++  java
  • 链表翻转系列

    Problem I

    翻转链表

    程序(简洁版)

    非递归

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

    Problem II: Swap Nodes in Pairs

    Given a linked list, swap every two adjacent nodes and return its head.

    For example,
    Given 1->2->3->4, you should return the list as 2->1->4->3.

    Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

    依次交换链表的一对节点。

    程序

    public class Solution {
        public ListNode swapPairs(ListNode head) {
    		ListNode dummy = new ListNode(-1);
    		ListNode pre = head;
    		ListNode connect = dummy;
    		
    		while (pre != null && pre.next != null) {
    			ListNode next = pre.next;
    			ListNode cross = next.next;
    			next.next = pre;
    			pre.next = cross;
    			connect.next = next;
    			connect = pre;
    			pre = cross;
    		}
    		
    		return dummy.next == null ? head : dummy.next;
    	}
    }
    

    Problem III: Reverse Nodes in k-Group

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.

    If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.

    You may not alter the values in the nodes, only nodes itself may be changed.

    Only constant memory is allowed.

    For example,
    Given this linked list: 1->2->3->4->5

    For k = 2, you should return: 2->1->4->3->5

    For k = 3, you should return: 3->2->1->4->5

    程序

    public class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            if (head == null || head.next == null || k <= 1) {
                return head;
            }
            
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode pre = dummy;
            ListNode cur = head;
            int cnt = 0;
            
            while (cur != null) {
                ++cnt;
                if (cnt % k == 0) {
                    // reverse group
                    ListNode last = reverseGroup(pre, cur.next);
                    pre = last;
                    cur = last.next;
                } else {
                    cur = cur.next;
                }
            }
            
            return dummy.next;
        }
        
        private ListNode reverseGroup(ListNode pre, ListNode next) {
            ListNode last = pre.next;
            ListNode cur = last.next;
            
            while (cur != next) {
                last.next = cur.next;
                cur.next = pre.next;
                pre.next = cur;
                cur = last.next;
            }
            
            return last;
        }
    }
    

    难点在于每次是翻转两个节点之间的节点们。

    Problem IV: Reverse LinkedList II

    Reverse a linked list from position m to n. Do it in-place and in one-pass.

    For example:
    Given 1->2->3->4->5->NULLm = 2 and n = 4,

    return 1->4->3->2->5->NULL.

    Note:
    Given mn satisfy the following condition:
    1 ≤ m ≤ n ≤ length of list.

    程序

    public class Solution {
        public ListNode reverseBetween(ListNode head, int m, int n) {
            if (head == null || head.next == null) {
                return head;
            }
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode pre = dummy;
            
            while (--m > 0) {
                pre = pre.next;
            }
            
            ListNode next = head;
            while (--n > 0) {
                next = next.next;
            }
            
            reverseBetween(pre, next.next);
            return dummy.next;
        }
        
        private void reverseBetween(ListNode pre, ListNode next) {
            ListNode last = pre.next;
            ListNode cur = last.next;
            
            while (cur != next) {
                last.next = cur.next;
                cur.next = pre.next;
                pre.next = cur;
                cur = last.next;
            }
        }
    }
    

    找准位置,套用问题3的方法即可~

  • 相关阅读:
    mysql-Invalid use of group function-聚合函数不能直接使用在where后面-使用exists,外查询与子查询应条件关联
    python-数据库之pymysql模块(连接对象-游标对象-执行-获取值或者提交事务)
    python作业-99乘法表作业,注意制表符合print结束符以及外层和里层怎么确定的,以及闰年
    python学习笔记-if_while_for_break_continue
    python-python中的Debug大法
    python-常用的几种格式化方法
    python学习一周总结
    python学习笔记-列表,字典,以及函数返回值
    python-注释,变量命名和规范笔记
    OpenJudge 求重要逆序对数
  • 原文地址:https://www.cnblogs.com/harrygogo/p/4716237.html
Copyright © 2011-2022 走看看