单链表反转问题

单链表反转问题

基本问题

如何将单链表反转?

算法实现

  1. /**
  2. *
  3. * Description: 单链表反转.
  4. *
  5. * @param head
  6. * @return ListNode
  7. */
  8. public static ListNode reverseList(ListNode head) {
  9. if (head == null) {
  10. return head;
  11. }
  12. ListNode prev = null;
  13. ListNode current = head;
  14. ListNode next = null;
  15. while (current != null) {
  16. next = current.next;
  17. current.next = prev;
  18. prev = current;
  19. current = next;
  20. }
  21. head = prev;
  22. return head;
  23. }

进阶问题

如何 将单链表在指定区间内进行反转?

问题分析

这个问题是上面问题的一个变形,难度也加大了不少,主要的难点之处就在于对边界条件的检查。

实现思路,主要就是按照给定的区间得到需要整体反转的一个子链表然后进行反转,最后就是把链表按正确的顺序拼接在一起。

算法实现

  1. /**
  2. *
  3. * Description: 单链表反转,反转指定区间内的节点.
  4. *
  5. * @param head
  6. * @param m
  7. * @param n
  8. * @return ListNode
  9. */
  10. public static ListNode reverseBetween(ListNode head, int m, int n) {
  11. // 合法性检测
  12. if (head == null || m >= n || m < 1 || n < 1) {
  13. return head;
  14. }
  15. /**
  16. * 将链表按[m,n]区间分成三段.
  17. *
  18. * first,second,third分别为每一段的头节点(注意,m=1也就是first与second相等的情况的处理)
  19. * first-->firstTail
  20. * second
  21. * third
  22. */
  23. ListNode first = head;
  24. ListNode firstTail = first;
  25. ListNode second = first;
  26. ListNode third = first;
  27. ListNode current = first;
  28. int i = 0;
  29. while (current != null) {
  30. i++;
  31. if (i == m - 1) {
  32. firstTail = current;
  33. }
  34. if (i == m) {
  35. second = current;
  36. }
  37. if (i == n) {
  38. third = current.next;
  39. break;
  40. }
  41. current = current.next;
  42. }
  43. // 进行中间second段的reverse
  44. current = second;
  45. ListNode prev = third;
  46. ListNode next = null;
  47. while (current != third) {
  48. next = current.next;
  49. current.next = prev;
  50. prev = current;
  51. current = next;
  52. }
  53. if (m == 1) {
  54. first = prev;
  55. } else {
  56. firstTail.next = prev;
  57. }
  58. return first;
  59. }

来自为知笔记(Wiz)

时间: 2024-08-09 06:34:29

单链表反转问题的相关文章

单链表反转的2种方法

1 public class ReverseDemo { 2 3 /** 4 * 单链表反转的两种方法 5 */ 6 public static void main(String[] args) { 7 Node head =new Node("a"); 8 Node node2=new Node("b"); 9 Node node3=new Node("c"); 10 head.setNext(node2); 11 node2.setNext(

链表 单链表反转

思路1:O(n^2). “狸猫换太子”,不进行改动链表结构,只首尾交换len/2次.但是在本函数中用到了定位函数,定位函数实际上是遍历了一遍整个链表,所以综合效率很低,达到O(n^2). //单链表反转(O(n^2)) void reverseList(Node* Head) { int count = numOfNodes(Head); //首尾交换 for(int i=1; i<=count/2; i++) { Node* p1 = locateNodeI(Head, i); Node* p

单链表反转C语言实现

单链表的反转可以使用循环,也可以使用递归的方式 1.循环反转单链表 循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可. 代码: # include <iostream> # include <cstdlib> using namespace std; struct linkNode { int val; linkNode *next; linkNode(int x):val(x),next(NULL){} }; linkNod

单链表反转python实现

单链表的反转可以使用循环,也可以使用递归的方式 1.循环反转单链表 循环的方法中,使用pre指向前一个结点,cur指向当前结点,每次把cur->next指向pre即可. 代码: class ListNode: def __init__(self,x): self.val=x; self.next=None; def nonrecurse(head): #循环的方法反转链表 if head is None or head.next is None: return head; pre=None; c

数据结构 单链表反转 回顾练习

之前写了一个单链表反转,但是使用的新的空间. 这次的反转是不修改原来的结构,直接将节点内的元素进行修改 1 #!/usr/bin/env python3 2 3 class LNode(object): 4 def __init__(self, elem, next_=None): 5 self.elem = elem 6 self.next = next_ 7 8 class ListError(Exception): 9 pass 10 11 class LList(object): 12

单链表反转(Singly Linked Lists in Java)

单链表反转(Singly Linked Lists in Java) 博客分类: 数据结构及算法 Java代码   package dsa.linkedlist; public class Node<E>{ E data; Node<E> next; } Java代码   package dsa.linkedlist; public class ReverseLinkedListRecursively { public static void main(String args[])

单链表反转java代码

据说单链表反转问题面试中经常问,而链表这个东西相对于数组的确稍微难想象,因此今天纪录一下单链表反转的代码. 1,先定义一个节点类. 1 public class Node { 2 int index; 3 Node next; 4 5 public Node(int index, Node next) { 6 this.index = index; 7 this.next = next; 8 } 9 } 2,我一共写了三种方法 (1)迭代法.先将下一节点纪录下来,然后让当前节点指向上一节点,再将

时间复杂度为O(n)的非递归单链表反转【算法导论课后题】

单链表反转:1->2->3->4... 思路:先将1指向3,2指向1,结果为2->1->3->4,然后循环将3插入到2之前 <span style="font-size:18px;">void reverseLinkedList(List head) { List tmp,p; if(head==null) { return ; } tmp=head->next; while(tmp->next !=null){ p=tmp-

单链表反转(2)

今天会介绍另外一种反转单链表的方法,对于单链表反转这一类的算法,最重要的思想就是用临时变量来记住需要记住的节点.一个不够,那就用两个,或者用三个临时变量. 这次还是以图表来表现算法的过程,不同于上一篇,这次的头节点使用链表中的第一个节点. 该方法的思想就是遍历每个节点,将其插入到第一个节点之后. 如下图为链表的初始状态: 为了不断链,先将 P2 的后驱节点指向 P3 的后驱节点. 将P3插入 P1 和 P2 之间,也就是 将 P1 的后驱指向 P3.将 P3 的后驱指向 P2 P3 指向 P2