Reverse Linked List I

Reverse a linked list.

Example

For linked list 1->2->3, the reversed linked list is 3->2->1

分析:

典型的3 pointers 问题。

  1. /**
  2. * Definition for ListNode.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int val) {
  7. * this.val = val;
  8. * this.next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param head: The head of linked list.
  15. * @return: The new head of reversed linked list.
  16. */
  17. public ListNode reverse(ListNode head) {
  18. if (head == null) return head;
  19. ListNode prev = null;
  20. ListNode cur = head;
  21. ListNode next = null;
  22. while (cur != null) {
  23. next = cur.next;
  24. cur.next = prev;
  25. prev = cur;
  26. cur = next;
  27. }
  28. return prev;
  29. }
  30. }

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.

Example

Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL.

分析:

我们需要把list分成三段。首先得到第一段的最后一个node,然后reverse中间部分。最后把三个部分链接起来。

  1. /**
  2. * Definition for ListNode
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. /**
  14. * @param ListNode head is the head of the linked list
  15. * @oaram m and n
  16. * @return: The head of the reversed ListNode
  17. */
  18. public ListNode reverseBetween(ListNode head, int m , int n) {
  19. if (m == n) return head;
  20. ListNode tempHead = new ListNode();
  21. tempHead.next = head;
  22.  
  23. // reach the end of the first part
  24. ListNode partEnd = tempHead;
  25. for(int k = ; k < m; k++) {
  26. partEnd = partEnd.next;
  27. }
  28. // save it later to connect to the last part.
  29. ListNode secondPartTail = partEnd.next;
  30.  
  31. // reverse the middle part
  32. ListNode prev = null;
  33. ListNode current = partEnd.next;
  34. ListNode next = null;
  35.  
  36. for (int p = ; p <= n - m + ; p++) {
  37. next = current.next;
  38. current.next = prev;
  39. prev = current;
  40. current = next;
  41. }
  42. // connect three parts
  43. partEnd.next = prev;
  44. secondPartTail.next = current;
  45.  
  46. return tempHead.next;
  47. }
  48. }

转载请注明出处:cnblogs.com/beiyeqingteng/

Reverse Linked List | & ||的更多相关文章

  1. [LeetCode] Reverse Linked List 倒置链表

    Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either i ...

  2. [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-> ...

  3. Leetcode-206 Reverse Linked List

    #206.  Reverse Linked List Reverse a singly linked list. /** * Definition for singly-linked list. * ...

  4. 迭代和递归 - leetcode 206. Reverse Linked List

    Reverse Linked List,一道有趣的题目.给你一个链表,输出反向链表.因为我用的是JavaScript提交,所以链表的每个节点都是一个对象.例如1->2->3,就要得到3-& ...

  5. 【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 ...

  6. [LintCode] Reverse Linked List 倒置链表

    Reverse a linked list. Have you met this question in a real interview? Yes Example For linked list 1 ...

  7. 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 ...

  8. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

  9. 【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 ...

随机推荐

  1. hdu1542矩阵的并 线段树+扫描线

    求矩阵的并,也就是要求所有的面积.那可以吧总的图形按照矩阵来切割.使其为一块一块. 输入的时候用坐标表示,这里扫描线从下到上扫描.初始时让下面的边为1,上面的为-1: 用一条先从下面开始想上扫描.遇到 ...

  2. FastDFS在centos上的安装配置与使用

    FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务.(百 ...

  3. Java编程思想学习(十六) 并发编程

    线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...

  4. 【bzoj2819】 Nim

    www.lydsy.com/JudgeOnline/problem.php?id=2819 (题目链接) 题意 动态树上路径异或和. Solution Nim取石子游戏的sg值就是每堆石子的异或和,所 ...

  5. MySQL------如何安装mysql-connector-java-5.1.38.zip

    下载地址:http://dev.mysql.com/downloads/connector/j/ 安装mysql-connector-java-5.1.38.zip:1.解压文件->把里面的my ...

  6. params参数的调用

    namespace params参数的用法 { class Program { public static void Test(string name,params int[] score) { ; ...

  7. 如何获取xcassets中LaunchImage图片

    NSDictionary * dic = @{@"320x480" : @"LaunchImage-700", @"320x568" : @ ...

  8. IIS负载均衡-Application Request Route详解第四篇:使用ARR实现三层部署架构(转载)

    IIS负载均衡-Application Request Route详解第四篇:使用ARR实现三层部署架构 系列文章链接: IIS负载均衡-Application Request Route详解第一篇: ...

  9. WPF MVVM模式

    1. MVVM MVVM的设计模式最早于2005年由微软的WPF和Silverlight架构师John Gossman在他的博客中提到. WPF中采用MVVM的架构可以获得以下好处: 1. 将UI和业 ...

  10. JavaScriptSerializer中日期序列化问题解决方案

    JavaScriptSerializer中日期序列化问题解决方案 直接进入主题: class Student { public int age { get; set; } public DateTim ...