`
huntfor
  • 浏览: 194789 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode]Reverse Linked List II

 
阅读更多
http://oj.leetcode.com/problems/reverse-linked-list-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->NULL, m = 2 and n = 4,

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

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

 在剑指offer中看到过反转链表,里面讲的思想稍稍复杂一点,是在原链表中进行反转操作,但是我拿到这道题的第一印象就是用头插法生成新的链表,并将新建链表查到原来链表的相应位置中。思想很简单明了,但是代码略长。

public ListNode reverseBetween(ListNode head, int m, int n) {
    	if(m == n || head == null){
    		return head;
    	}
    	//------ 手动添加头结点,为了操作统一性
    	ListNode phead = new ListNode(0);
    	phead.next = head;
    	ListNode tem = phead;
    	
    	int count = 0;
    	//需要记录m ,n前后的两个节点,方便新链表的插入
    	ListNode newList = new ListNode(0);
    	ListNode mPreNode = null;
    	ListNode nPreNode = null;
    	ListNode tail = null;
    	
    	while(tem != null) {
    		if(count == m - 1){
    			if(m == 1){
					mPreNode = phead;
    			}else{
    				mPreNode = tem;
    			}
    		}else if (count == n + 1){
    			nPreNode = tem;
    		}else if(count <= n && count >= m){
                        //头插法建表
    			ListNode node = new ListNode(tem.val);
    			node.next = newList.next;
    			newList.next = node;
    			if(count == m){
    				tail = node;
    			}
    		}
    		count++;
    		tem = tem.next;
    	}
    	mPreNode.next = newList.next;
    	tail.next = nPreNode;
    	return phead.next;
    }

 

分享到:
评论

相关推荐

    fuxuemingzhu#Leetcode-Solution-All#92. Reverse Linked List II 反转

    进行一次遍历,把第m到n个元素进行翻转,即依次插入到第m个节点的头部。这个题还是有意思的。建议后面再多做几遍。Python代码如下:self.next = No

    leetcode盒子嵌套-leetcode-text:leetcode-文本

    leetcode盒子嵌套 leetcode-text 92.Reverse Linked List II Runtime: 4 ms, faster than 67.04% of C online submissions for Reverse Linked List II. Memory Usage: 6.9 MB, less than 100.00% of C online ...

    leetcode中文版-LeetCode:力码

    92.Reverse Linked List II LeetCode 138.Copy List with Random Pointer LeetCode 142.Linked List Cycle II(solve1) LeetCode 142.Linked List Cycle II(solve2) LeetCode 160.Intersection of Two Linke

    LeetCode最全代码

    * [Linked List](https://github.com/kamyu104/LeetCode#linked-list) * [Stack](https://github.com/kamyu104/LeetCode#stack) * [Queue](https://github.com/kamyu104/LeetCode#queue) * [Heap]...

    leetcode不会-Leetcode-Java:Leetcode-Java

    leetcode 不会 Leetcode Solutions in Java Linked List Linked List ...linked list, ...快慢指针法,块指针从head.next开始,慢指针从head开始,快指针每次移动两格,慢...reverseList(ListNode head) 三个指针,依次往后

    LeetCode2 Add Two Numbers

    You are given two non-empty linked lists ... Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. java AC版本

    leetcode2sumc-LeetCode:LeetCode的一些题目

    leetcode 2 sum c LeetCode 帮助文档 帮助文档存放在Help文件夹下。 文件名 文件描述 ...complexitypython.txt Python的一些常规操作的复杂度统计 ...Reverse Linked List 234 Easy Palindrome Linked List

    Leetcode 反转链表.js

    LeetCode 206的题目是“反转链表”(Reverse Linked List),它要求将一个单链表的所有节点反转,并返回反转后链表的头节点。这是一个基础但非常重要的链表操作问题,它不仅考察了对链表数据结构的理解,还涉及到了...

    leetcode中325题python-leetcode:leetcode

    reverse-linked-list-ii(Reverse a Sub-list) 141 环形链表 linked-list-cycle 142 环形链表 II linked-list-cycle-ii 143 重排链表 reorder-list 148 排序链表 sort-list 234 回文链表 palindrome-linked-list 双...

    tech.github.io:我的博客

    终生成长 :hot_beverage: 为什么要建这个仓库 梳理自己掌握的知识点,整理自己的知识体系。... Reverse Linked ListLeetcode 141. Linked List CycleLeetcode 21. Merge Two Sorted ListsLeetCode 224. Basic Cal

    leetcodepython001-Algorithm:关于数据结构和算法的问题(Leetcode、编程之美和课程作业)

    leetcode python 001 Algorithm 用python和java解决了Leetcode上部分问题,另外还有对常规算法和机器学习算法...Linked List II Python 2017/11/28 Medium 095 Unique Binary Search Trees Python 2017/11/28 Medium 09

    leetcode分发糖果-ForDaFactory:使用C++的个人leetcode解决方案

    92-反转链表II:reverse-linked-listt-ii 141-环形链表:linked-list-cycle 142-环形链表:linked-list-cycle-ii 160-相交链表:intersection-of-two-linked-lists 206-反转一个单链表:reverse-linked-list 20-有效的...

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    leetcode分类-leetcode-go:LeetCode刷题记录

    Linked List O(n) O(1) Sliding Window O(n) O(n) Binary-Search O(log (m+n)) O(1) GO JAVA DP String O(n) O(n) [GO](. Reverse Integer/ri7.go) [JAVA](. Reverse Integer/Solution.java) Math O(log(x)) O(1) ...

    cpp-算法精粹

    Reverse Linked List II Partition List Remove Duplicates from Sorted List Remove Duplicates from Sorted List II Rotate List Remove Nth Node From End of List Swap Nodes in Pairs Reverse Nodes in k-Group...

    leetcode跳跃-LeetCode:力扣刷题70道!

    Reverse Linked List 队列 Queue 力扣 933 最近的请求次数 | Number of Recent Calls 力扣 225 用队列实现栈 | Implement Stack Using Queue 力扣 622 设计循环队列 | Design Circular Queue 力扣 641 设计循环双端...

    leetcode338-Leetcode:一些过滤leetcode问题的解决方案

    Leetcode 问题的 Python 解决方案 下面列出了这些问题的概述。 这些问题主要分为数据结构和算法两部分。 代码可以在此存储库的相应文件夹中找到。 数据结构: 细绳: 问题及解决方法: 242.有效字谜:| 409.最长回文...

    leetcodepushfront-leetcode:leetcode问题

    leetcode 推前当前问题 5 从 9 月 9 日到 12 月 9 日,按类型。 100 个主题和 Google。 力码 Leetcode 题目汇总 分类 1、求和问题 1.1 (1) Two Sum 1.2 (15) ...II ...II ...Reverse Linked List 概括 对于

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    LeetCode-NoteBook:docsifyjs

    Reverse Linked List中等的33. Search in Rotated Sorted Array74. Search a 2D Matrix I240. Search a 2D Matrix II2. Add Two Numbers50. Pow(x, n)34. First & LastPositionElementInSortedArr94. Binary Tree ...

Global site tag (gtag.js) - Google Analytics