leetcode-005 reorder list

 1 package leetcode;
 2
 3 public class ReOrderList {
 4     public void reorderList(ListNode head) {
 5         if(head==null||head.next==null||head.next.next==null){
 6
 7         }else{
 8             int l=numNode(head);
 9             ListNode mid = new ListNode(-1);
10             mid=getMid(head);
11             ListNode next=mid.next;
12             ListNode po=reverse(next);
13             mid.next=null;
14             ListNode p=head;
15             while(po!=null){
16                 ListNode po2=po.next;
17                 po.next=p.next;
18                 p.next=po;
19                 p=p.next.next;
20                 po=po2;
21             }
22         }
23
24     }
25     public int numNode(ListNode head){
26         if(head==null){
27             return 0;
28         }
29         int i=0;
30         while(head!=null){
31             i++;
32             head=head.next;
33         }
34         return i;
35     }
36     public ListNode getMid(ListNode head){
37         ListNode p=head;
38         ListNode q=head;
39         ListNode pre=head;
40         while(q.next!=null&&q.next.next!=null){
41             pre=p;
42             p=p.next;
43             q=q.next.next;
44         }
45         return p;
46     }
47     public ListNode reverse(ListNode n){
48         ListNode h = new ListNode(-1);
49         ListNode q = n;
50         while(q!=null){
51             ListNode next=q.next;
52             q.next=h.next;
53             h.next=q;
54             q=next;
55         }
56         return h.next;
57     }
58     public static void main(String[] args){
59         ListNode a=new ListNode(1);
60         ListNode b=new ListNode(2);
61         ListNode c=new ListNode(3);
62         ListNode d=new ListNode(4);
63         a.next=b;
64         b.next=c;
65         c.next=d;
66         d.next=null;
67         ReOrderList s = new ReOrderList();
68         s.reorderList(a);
69         ListNode p=a;
70         while (p != null) {
71             System.out.println(p.val);
72             p = p.next;
73         }
74     }
75
76 }

leetcode-005 reorder list

时间: 2024-10-15 04:42:59

leetcode-005 reorder list的相关文章

[LeetCode] 005. Longest Palindromic Substring (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 005.Longest_Palindromic_Substring (Medium) 链接: 题目:https://oj.leetcode.com/problems/Longest-Palindromic-Substring/ 代码(github):https://github.com/illuz/leetcode

【Leetcode】Reorder List JAVA

一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 二.分析 1.暴力解法 这种解法所需的时间的时间复杂度比较高,为O(n2) 代码如下

[LeetCode] String Reorder Distance Apart

Question: Given a string of lowercase characters, reorder them such that the same characters are at least distance d from each other. Input: { a, b, b }, distance = 2 Output: { b, a, b } http://leetcode.com/2010/05/here-is-another-google-phone-interv

[LeetCode] 937. Reorder Data in Log Files 日志文件的重新排序

You have an array of?`logs`.? Each log is a space delimited string of words. For each log, the first word in each log is an alphanumeric?identifier.? Then, either: Each word after the identifier will consist only of lowercase letters, or; Each word a

leetcode 【 Reorder List 】python 实现

题目: Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 代码: oj 测试通过 248 ms 1 # Definition for singly-

[Leetcode][JAVA] Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 比较容易思考且实现的一个思路是, 将链表从中心拆成两半,后一半全部反向连接,然后前一半和后一半一个

Java for LeetCode 143 Reorder List

Given a singly linked list L: L0→L1→…→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. 解题思路一: 每次将Ln换到前面,得到L0→Ln→L1→L2→L3→,然后对L1使用相同操作,

【LeetCode】Reorder List

Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * clas

【leetcode】Reorder List (middle)

Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 先把链表分成两节,后半部分翻转,然后前后交叉连接. 大神的代码比我的简洁,注意分两节时用快

【LeetCode】Reorder List 解题报告

Given a singly linked list L: L0→L1→-→Ln-1→Ln, reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→- You must do this in-place without altering the nodes' values. For example, Given {1,2,3,4}, reorder it to {1,4,2,3}. /** * Definition for singly-linked list. * clas