Reverse Linked List | & ||
Reverse Linked List I
Reverse a linked list.
For linked list 1->2->3
, the reversed linked list is 3->2->1
分析:
典型的3 pointers 问题。
- /**
- * Definition for ListNode.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int val) {
- * this.val = val;
- * this.next = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param head: The head of linked list.
- * @return: The new head of reversed linked list.
- */
- public ListNode reverse(ListNode head) {
- if (head == null) return head;
- ListNode prev = null;
- ListNode cur = head;
- ListNode next = null;
- while (cur != null) {
- next = cur.next;
- cur.next = prev;
- prev = cur;
- cur = next;
- }
- return prev;
- }
- }
Reverse Linked List II
Reverse a linked list from position m to n.
Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list.
Given 1->2->3->4->5->NULL
, m = 2
and n = 4
, return 1->4->3->2->5->NULL
.
分析:
我们需要把list分成三段。首先得到第一段的最后一个node,然后reverse中间部分。最后把三个部分链接起来。
- /**
- * Definition for ListNode
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) {
- * val = x;
- * next = null;
- * }
- * }
- */
- public class Solution {
- /**
- * @param ListNode head is the head of the linked list
- * @oaram m and n
- * @return: The head of the reversed ListNode
- */
- public ListNode reverseBetween(ListNode head, int m , int n) {
- if (m == n) return head;
- ListNode tempHead = new ListNode();
- tempHead.next = head;
- // reach the end of the first part
- ListNode partEnd = tempHead;
- for(int k = ; k < m; k++) {
- partEnd = partEnd.next;
- }
- // save it later to connect to the last part.
- ListNode secondPartTail = partEnd.next;
- // reverse the middle part
- ListNode prev = null;
- ListNode current = partEnd.next;
- ListNode next = null;
- for (int p = ; p <= n - m + ; p++) {
- next = current.next;
- current.next = prev;
- prev = current;
- current = next;
- }
- // connect three parts
- partEnd.next = prev;
- secondPartTail.next = current;
- return tempHead.next;
- }
- }
转载请注明出处:cnblogs.com/beiyeqingteng/
Reverse Linked List | & ||的更多相关文章
- [LeetCode] Reverse Linked List 倒置链表
Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...
- [LeetCode] 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-> ...
- Leetcode-206 Reverse Linked List
#206. Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...
- 迭代和递归 - leetcode 206. Reverse Linked List
Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...
- 【leetcode】Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- [LintCode] Reverse Linked List 倒置链表
Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...
- 14. Reverse Linked List II
Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. F ...
- 【12_206】Reverse Linked List
本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
随机推荐
- hdu1542矩阵的并 线段树+扫描线
求矩阵的并,也就是要求所有的面积.那可以吧总的图形按照矩阵来切割.使其为一块一块. 输入的时候用坐标表示,这里扫描线从下到上扫描.初始时让下面的边为1,上面的为-1: 用一条先从下面开始想上扫描.遇到 ...
- FastDFS在centos上的安装配置与使用
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务.(百 ...
- Java编程思想学习(十六) 并发编程
线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...
- 【bzoj2819】 Nim
www.lydsy.com/JudgeOnline/problem.php?id=2819 (题目链接) 题意 动态树上路径异或和. Solution Nim取石子游戏的sg值就是每堆石子的异或和,所 ...
- MySQL------如何安装mysql-connector-java-5.1.38.zip
下载地址:http://dev.mysql.com/downloads/connector/j/ 安装mysql-connector-java-5.1.38.zip:1.解压文件->把里面的my ...
- params参数的调用
namespace params参数的用法 { class Program { public static void Test(string name,params int[] score) { ; ...
- 如何获取xcassets中LaunchImage图片
NSDictionary * dic = @{@"320x480" : @"LaunchImage-700", @"320x568" : @ ...
- IIS负载均衡-Application Request Route详解第四篇:使用ARR实现三层部署架构(转载)
IIS负载均衡-Application Request Route详解第四篇:使用ARR实现三层部署架构 系列文章链接: IIS负载均衡-Application Request Route详解第一篇: ...
- WPF MVVM模式
1. MVVM MVVM的设计模式最早于2005年由微软的WPF和Silverlight架构师John Gossman在他的博客中提到. WPF中采用MVVM的架构可以获得以下好处: 1. 将UI和业 ...
- JavaScriptSerializer中日期序列化问题解决方案
JavaScriptSerializer中日期序列化问题解决方案 直接进入主题: class Student { public int age { get; set; } public DateTim ...