编写函数,检查链表是否为回文

 

题解:

判断一个链表是不是回文的,这里要求O(n)时间复杂度和O(1)的空间时间复杂度,总共想了三种办法,三种办法都用到了两个指针,符合题目要求的只有最后一种。

 

第一种办法:用数组倒着存前半段的链表的值,然后和后半段链表的值进行比较。这种解法运行的时间最久可能是因为数组倒着插入比较耗时。

代码:

 

[java] view plain copy

 

  1. //用数组实现 o(n/2)空间  
  2.      public static boolean isPalindrome(ListNode head) {  
  3.           
  4.         // ArrayList<Integer> nodeVal=new ArrayList<>();  
  5.          LinkedList<Integer> nodeVal=new LinkedList<>();  
  6.            
  7.          if(head==null||head.next==null)  
  8.              return true;  
  9.          ListNode slow=head;  
  10.          ListNode fast=head;  
  11.            
  12.          nodeVal.add(0,slow.val);  
  13.          while(fast.next!=null&&fast.next.next!=null)  
  14.          {  
  15.              fast=fast.next.next;  
  16.              slow=slow.next;  
  17.              nodeVal.add(0,slow.val);  
  18.          }  
  19.            
  20.          ListNode cur=slow;  
  21.          if(fast.next!=null)//链表长度为偶数  
  22.              cur=slow.next;  
  23.          int i=0;  
  24.          while(cur!=null)  
  25.          {  
  26.              if(nodeVal.get(i)!=cur.val)  
  27.                  return false;  
  28.              cur=cur.next;  
  29.              i++;  
  30.          }  
  31.          return true;  
  32.         }  
  33.        

第二种解法:在第一种的思路的基础上,我们要实现一个倒序,我们干嘛不用现成的数据结构-栈,于是把链表前半段压栈,然后出栈和后面的链表依次比较,这种运行时间最短,但因为用到了栈还是不符合题目要求。

 

代码:

 

[java] view plain copy

 

  1. //用栈实现  
  2.  public static boolean isPalindrome2(ListNode head) {  
  3.        
  4.      Stack<ListNode> stack=new Stack<>();  
  5.      ListNode slow=head;  
  6.      ListNode fast=head;  
  7.        
  8.      if(fast==null||fast.next==null)//0个节点或是1个节点  
  9.          return true;  
  10.   
  11.      stack.push(slow);  
  12.      while(fast.next!=null&&fast.next.next!=null)  
  13.      {  
  14.           
  15.          fast=fast.next.next;  
  16.          slow=slow.next;  
  17.          stack.push(slow);  
  18.      }  
  19.      if(fast.next!=null)//链表长度为偶数  
  20.          slow=slow.next;  
  21.        
  22.      ListNode cur=slow;  
  23.      while(cur!=null)  
  24.      {  
  25.          if(cur.val!=stack.pop().val)  
  26.              return false;  
  27.          cur=cur.next;  
  28.      }  
  29.      return true;  
  30.    
  31.  }  


第三种:我们这样想,我们可不可以不借助外在的存储实现倒序呢,其实是可以的,链表反转的时候我们就没有借助外在存储。思路是把后半段的原地链表反转然后和前半段进行比较(当然你也可以反转前半段)运行时间稍微比第二种慢一些,但是符合题目O(1)空间复杂度的要求

 

代码:

 

[java] view plain copy

 

  1. //链表原地转置实现o(1)空间复杂度  
  2. public static boolean isPalindrome3(ListNode head) {  
  3.  ListNode slow=head;  
  4.  ListNode fast=head;  
  5.    
  6.  if(fast==null||fast.next==null)//0个节点或是1个节点  
  7.      return true;  
  8.   
  9.   
  10.  while(fast.next!=null&&fast.next.next!=null)  
  11.  {  
  12.      fast=fast.next.next;  
  13.      slow=slow.next;  
  14.  }  
  15.  //对链表后半段进行反转  
  16.  ListNode midNode=slow;  
  17.  ListNode firNode=slow.next;//后半段链表的第一个节点  
  18.  ListNode cur=firNode.next;//插入节点从第一个节点后面一个开始  
  19.  firNode.next=null;//第一个节点最后会变最后一个节点  
  20.  while(cur!=null)  
  21.  {  
  22.      ListNode nextNode=cur.next;//保存下次遍历的节点  
  23.      cur.next=midNode.next;  
  24.      midNode.next=cur;  
  25.      cur=nextNode;  
  26.  }  
  27.    
  28.  //反转之后对前后半段进行比较  
  29.  slow=head;  
  30.  fast=midNode.next;  
  31.  while(fast!=null)  
  32.  {  
  33.      if(fast.val!=slow.val)  
  34.          return false;  
  35.      slow=slow.next;  
  36.      fast=fast.next;  
  37.  }  
  38.  return true;  
  39.    
  40. }  

快行指针找到链表中间结点

1. 反转前半部分看是否和后半部分一样

2. 将前半部分入栈,迭代访问剩下的一半结点,每次的栈顶元素一样则是回文链表

 

 

[java] view plain copy

 

  1. import java.util.Stack;  
  2. public class isHuiWen {  
  3.     public boolean isPalinddrome(LinkedListNode head) {  
  4.         LinkedListNode fast = head;  
  5.         LinkedListNode slow = head;  
  6.           
  7.         Stack<Integer> stack = new Stack<Integer>();  
  8.           
  9.         while( fast != null && fast.next != null ) {  
  10.             stack.push(slow.data);  
  11.             slow = slow.next;  
  12.             fast = fast.next.next;  
  13.         }  
  14.           
  15.         //如果链表有奇数个元素,那么fast这时不为空,则比较后半段时跳过中间元素  
  16.         if ( fast != null ) {  
  17.             slow = slow.next;  
  18.         }  
  19.           
  20.         while (slow != null) {  
  21.             int top = stack.pop().intValue();  
  22.             //如果不相同,则不是回文  
  23.             if (top != slow.data) {  
  24.                 return false;  
  25.             }  
  26.             slow= slow.next;  
  27.         }  
  28.         return true;  
  29.     }  
  30. }  


 

递归的解法:

 

[java] view plain copy

 

  1. class Result{  
  2.     public LinkedListNode node;  
  3.     public boolean result;  
  4. }  
  5.   
  6. Result isPalindromeRecurse(LinkedListNode head, int length) {  
  7.     if (head == null || length == 0) {  
  8.         return new Result(null, true);  
  9.     }  
  10.     else if(length == 1) {  
  11.         return new Result(head.next,true);  
  12.     }  
  13.     else if(length == 2) {  
  14.         return new Result(head.next.next, head.data == head.next.data);  
  15.     }  
  16.     Result res = isPalindromeRecurse(head.next, length -2);  
  17.     if(!res.result || res.node == null){  
  18.         return res;  
  19.     }  
  20.     else{  
  21.         res.result = head.data == res.node.data;  
  22.         res.node = res.node.next;  
  23.         return res;  
  24.     }  
  25. }  
  26.   
  27. boolean isPalinddrome(LinkedListNode head) {  
  28.     Result p = isPalindromeRecurse(head, listSize(head));  
  29.     return p.result;  

思路:

1 Iterative,利用栈,把链表前半段存入栈,再逐个弹栈和链表后半段比较。注意链表长度为奇数的情况!要跳过中间节点!

2 递归!

定义递归函数为 Result rec(LinkedListNode head, int length)  意义为 传入链表头结点和链表长度,返回该链表的尾节点的下一个节点

Result是一个Wrapper 类,包含了node和当前是否match的判断

递归的关键是要清楚递归函数的每一个参数的意义是什么,还有返回值的意义是什么!!!

如下图:假设在递归的某一个阶段,要比较前半段的那个1和后半段的那个1是否相等:

根据递归可以得到蓝色部分的返回值Result,包含了一个res.node和match值。res.node的意义就是该子链表的尾节点的下一个节点,即后半段的1!

然后我们可以把head指向的1和res.node指向的1比较。如果相同则设置match为true,并且更新本层递归(红色区域)的返回值为本层子链表的尾节点(1)的下一个节点(0),作为res.node返回。

 

 

 

[java] view plain copy

在CODE上查看代码片派生到我的代码片

  1. package LinkLists;  
  2.   
  3. import java.util.Stack;  
  4.   
  5. import CtCILibrary.LinkedListNode;  
  6.   
  7. public class S2_7 {  
  8.   
  9.     // 利用栈,把链表前半段存入栈,再逐个弹栈和链表后半段比较  
  10.     public static boolean isPalindrome(LinkedListNode head) {  
  11.         LinkedListNode fast = head;  
  12.         LinkedListNode slow = head;  
  13.   
  14.         Stack<Integer> stack = new Stack<Integer>();  
  15.   
  16.         while (fast != null && fast.next != null) {  
  17.             stack.push(slow.data);  
  18.             slow = slow.next;  
  19.             fast = fast.next.next;  
  20.         }  
  21.   
  22.         if (fast != null) { // 只有当链表长度为奇数时,fast才不会为null  
  23.             slow = slow.next; // 这时要跳过中间节点  
  24.         }  
  25.   
  26.         while (slow != null) { // 边弹栈边比较  
  27.             int top = stack.pop().intValue();  
  28.             if (top != slow.data) {  
  29.                 return false;  
  30.             }  
  31.             slow = slow.next;  
  32.         }  
  33.         return true;  
  34.     }  
  35.   
  36.     // 递归  
  37.     public static boolean isPalindrome2(LinkedListNode head) {  
  38.         int size = 0;  
  39.         LinkedListNode n = head;  
  40.         while (n != null) {  
  41.             size++;  
  42.             n = n.next;  
  43.         }  
  44.         Result p = rec(head, size);  
  45.         return p.match;  
  46.     }  
  47.   
  48.     // 传入链表头结点和链表长度,返回该链表的尾节点的下一个节点  
  49.     public static Result rec(LinkedListNode head, int length) {  
  50.         if (head == null || length == 0) {  // 空链表,肯定是回文  
  51.             return new Result(null, true);  
  52.         } else if (length == 1) {       // 只有1个节点,肯定是回文  
  53.             return new Result(head.next, true);  
  54.         } else if (length == 2) {       // 有两个节点,如果相同则是回文  
  55.             return new Result(head.next.next, head.data == head.next.data);  
  56.         }  
  57.           
  58.         Result res = rec(head.next, length-2);      // 长度缩小2的子链表问题,res存放子问题的结果和子链表尾节点的下一个节点  
  59.         if(!res.match || res.node==null) {          // 不match  
  60.             return res;  
  61.         } else{  
  62.             res.match = head.data == res.node.data;     // 比较当前节点和尾节点是否相等  
  63.             res.node = res.node.next;                           // 更新返回值,即该链表的尾节点的下一个节点  
  64.             return res;  
  65.         }  
  66.     }  
  67.       
  68.     static class Result {  
  69.         public LinkedListNode node;  
  70.         public boolean match;  
  71.         public Result(LinkedListNode n, boolean res) {  
  72.             node = n;  
  73.             match = res;  
  74.         }  
  75.     }  
  76.       
  77.       
  78.     public static void main(String[] args) {  
  79.         int length = 10;  
  80.         LinkedListNode[] nodes = new LinkedListNode[length];  
  81.         for (int i = 0; i < length; i++) {  
  82.             nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);  
  83.         }  
  84.   
  85.         for (int i = 0; i < length; i++) {  
  86.             if (i < length - 1) {  
  87.                 nodes[i].setNext(nodes[i + 1]);  
  88.             }  
  89.             if (i > 0) {  
  90.                 nodes[i].setPrevious(nodes[i - 1]);  
  91.             }  
  92.         }  
  93. //       nodes[length - 2].data = 9; // Uncomment to ruin palindrome  
  94.   
  95.         LinkedListNode head = nodes[0];  
  96.         System.out.println(head.printForward());  
  97.         System.out.println(isPalindrome2(head));  
  98.     }  
  99.   

转载于:https://my.oschina.net/u/2822116/blog/789172

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值