1. 程式人生 > >每天一道leetcode234-迴文連結串列

每天一道leetcode234-迴文連結串列

考試結束,班級平均分只拿到了年級第二,班主任於是問道:大家都知道世界第一高峰珠穆朗瑪峰,有人知道世界第二高峰是什麼嗎?正當班主任要繼續發話,只聽到角落默默想起來一個聲音:”喬戈裡峰”

前言

2018.11.6號打卡

明天的題目:https://leetcode-cn.com/problems/remove-linked-list-elements/
以後明天的題目提取公佈一下哈,因為有些朋友想提前做一下~

題目

leetcode234-迴文連結串列
中文連結:
https://leetcode-cn.com/problems/palindrome-linked-list/
英文連結串列:
https://leetcode.com/problems/palindrome-linked-list/
難度:easy
分類:連結串列

題目詳述

請判斷一個連結串列是否為迴文連結串列。

示例 1:

輸入: 1->2
輸出: false
示例 2:

輸入: 1->2->2->1
輸出: true

題目詳解

距離AC只差一個測試用例的錯誤思路

  • 之前應該有看過關於迴文連結串列的一種解法,就是對於連結串列的每個元素依次乘以1,2,3,4…求得一個和sum1;
  • 然後就是把這個連結串列反轉,反轉連結串列正好昨天做過哈,直接把程式碼拿來用,得到反轉後的連結串列;
  • 然後對於這個反轉後的連結串列,依次遍歷然後對於每個元素依次乘以1,2,3,4…求得一個和sum2;
  • 然後比較這個兩個sum值,如果相等,那麼就是迴文連結串列

程式碼

 1/**
2 * Definition for singly-linked list.
3 * public class ListNode {
4 *     int val;
5 *     ListNode next;
6 *     ListNode(int x) { val = x; }
7
 * }
8 */

9class Solution {
10    public boolean isPalindrome(ListNode head) {
11        int sum1 = 0;
12        if(head == null || head.next == null)
13            return true;
14        int count = 1;
15        ListNode temp = head;
16        while(temp != null)
17        {
18            sum1 += count * temp.val;
19            count += 1;
20            temp = temp.next;
21        }
22        int sum2 = 0;
23        count = 1;
24        head = reverseList(head);
25        temp = head;
26        while(temp != null)
27        {
28            sum2 += count * temp.val;
29            count += 1;
30            temp = temp.next;
31        }
32        if(sum1 == sum2)
33            return true;
34        return false;
35    }
36    public ListNode reverseList(ListNode head) {
37        if(head == null || head.next == null)
38            return head;
39        ListNode pre = head;
40        ListNode pNode = head.next;
41        ListNode next = head;
42        //首先處理前兩個節點;
43        pre.next = null;
44        while(pNode != null)
45        {
46            next = pNode.next;
47            pNode.next = pre;
48            pre = pNode;
49            pNode = next;
50        }
51        return pre;
52    }
53}

結果,差一個用例沒過,說明這種方法還是有點問題~~~~

dasda

正確的思路

  • 由於題目說了時間複雜度是O(n),空間複雜度是O(1),所以不能使用新的空間;
  • 思路還是反轉連結串列,不過不是反轉整個連結串列,反轉的是後半部分的連結串列;
  • 後半部分的連結串列反轉完畢,然後一個從頭開始遍歷,一個從尾巴開始遍歷,依次比較節點的值是不是一樣,一樣就繼續往下,不一樣直接就返回false.

程式碼

 1/**
2 * Definition for singly-linked list.
3 * public class ListNode {
4 *     int val;
5 *     ListNode next;
6 *     ListNode(int x) { val = x; }
7 * }
8 */

9class Solution {
10    public boolean isPalindrome(ListNode head) {
11        if(head == null || head.next == null)
12            return true;
13        int length = 0;
14        ListNode temp = head;
15        while(temp != null)
16        {
17            length++;
18            temp = temp.next;
19        }
20        int halfLength = length / 2;
21        temp = head;
22        for(int i=0;i<halfLength;i++)
23            temp = temp.next;
24        ListNode pre = temp;
25        ListNode pNode = temp.next;
26        ListNode next = pNode;
27        while(pNode != null)
28        {
29            next = pNode.next;
30            pNode.next = pre;
31            pre = pNode;
32            pNode = next;
33        }
34        for(int i=0;i<halfLength;i++)
35        {
36            if(head.val != pre.val)
37                return false;
38            head = head.next;
39            pre = pre.next;
40        }
41        return true;
42    }
43}

程式碼講解

  • 15到20行,遍歷連結串列,求連結串列長度的一半的值
  • 22-23行,找到連結串列的中間節點
  • 24-33行反轉連結串列
  • 34-40行一個從頭,一個從尾巴,依次比較值是否相等,不相等就返回false
  • 最後就是返回true

結束語

2018.11.6號 打卡

作者喬戈裡親歷2019秋招,哈工大計算機本碩,百度准入職java工程師,歡迎大家關注我的微信公眾號:程式設計師喬戈裡,公眾號有3T程式設計資源,以及我和我朋友(准入職百度C++工程師)在秋招期間整理的近200M的面試必考的java與C++面經,並有每天一道leetcode打卡群與技術交流群,歡迎關注。