LeetCode234_PalindromeLinkedList (推斷是否為迴文鏈表) Java題解

來源:互聯網
上載者:User

標籤:time   i++   isp   執行   do it   val   lis   node   integer   

題目:

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

題解:

推斷一個鏈表是不是迴文的,這裡要求O(n)時間複雜度和O(1)的空間時間複雜度。總共想了三種辦法,三種辦法都用到了兩個指標,符合題目要求的僅僅有最後一種。


第一種辦法:用數組倒著存前半段的鏈表的值。然後和後半段鏈表的值進行比較。

這樣的解法執行的時間最久可能是由於數組倒著插入比較耗時。

代碼:

//用數組實現 o(n/2)空間 public static boolean isPalindrome(ListNode head) {// ArrayList<Integer> nodeVal=new ArrayList<>(); LinkedList<Integer> nodeVal=new LinkedList<>();  if(head==null||head.next==null) return true; ListNode slow=head; ListNode fast=head;  nodeVal.add(0,slow.val); while(fast.next!=null&&fast.next.next!=null) { fast=fast.next.next; slow=slow.next; nodeVal.add(0,slow.val); }  ListNode cur=slow; if(fast.next!=null)//鏈表長度為偶數 cur=slow.next; int i=0; while(cur!=null) { if(nodeVal.get(i)!=cur.val) return false; cur=cur.next; i++; } return true;    } 
另外一種解法:在第一種的思路的基礎上。我們要實現一個倒序,我們幹嘛不用現成的資料結構-棧。於是把鏈表前半段壓棧,然後出棧和後面的鏈表依次比較,這樣的執行時間最短。但由於用到了棧還是不符合題目要求。

代碼:

 //用棧實現 public static boolean isPalindrome2(ListNode head) {  Stack<ListNode> stack=new Stack<>(); ListNode slow=head; ListNode fast=head;  if(fast==null||fast.next==null)//0個節點或是1個節點 return true; stack.push(slow); while(fast.next!=null&&fast.next.next!=null) { fast=fast.next.next; slow=slow.next; stack.push(slow); } if(fast.next!=null)//鏈表長度為偶數 slow=slow.next;  ListNode cur=slow; while(cur!=null) { if(cur.val!=stack.pop().val) return false; cur=cur.next; } return true;  }

第三種:我們這樣想,我們可不能夠不藉助外在的儲存實現倒序呢,事實上是能夠的,鏈表反轉的時候我們就沒有藉助外在儲存。

思路是把後半段的原地鏈表反轉然後和前半段進行比較(當然你也能夠反轉前半段)執行時間略微比另外一種慢一些。可是符合題目O(1)空間複雜度的要求

代碼:

 //鏈表原地轉置實現o(1)空間複雜度 public static boolean isPalindrome3(ListNode head) { ListNode slow=head; ListNode fast=head;  if(fast==null||fast.next==null)//0個節點或是1個節點 return true; while(fast.next!=null&&fast.next.next!=null) { fast=fast.next.next; slow=slow.next; } //對鏈表後半段進行反轉 ListNode midNode=slow; ListNode firNode=slow.next;//後半段鏈表的第一個節點 ListNode cur=firNode.next;//插入節點從第一個節點後面一個開始 firNode.next=null;//第一個節點最後會變最後一個節點 while(cur!=null) { ListNode nextNode=cur.next;//儲存下次遍曆的節點 cur.next=midNode.next; midNode.next=cur; cur=nextNode; }  //反轉之後對前後半段進行比較 slow=head; fast=midNode.next; while(fast!=null) { if(fast.val!=slow.val) return false; slow=slow.next; fast=fast.next; } return true;  }




LeetCode234_PalindromeLinkedList (推斷是否為迴文鏈表) Java題解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.