2.2.3 Partition List
来源:互联网 发布:浙江外商直接投资数据 编辑:程序博客网 时间:2024/05/16 17:30
Link: https://oj.leetcode.com/problems/partition-list/
这是CC150原题,我的思路正确,但代码有问题。再做。
Time: O(n), Space: O(1)
我的代码:
public class Solution { //CC150 public ListNode partition(ListNode head, int x) { if(head == null) return null; ListNode sHead = null;//small ListNode sTail = null; ListNode lHead = null;//large ListNode lTail = null; while(head != null){ if(head.val < x){ sTail.next = head; sTail = sTail.next; } else{ lTail.next = head; lTail = lTail.next; } head = head.next; } sTail.next = lHead; return sHead.next; }}
正确答案:
主要错误是:
sTail.next = lHead;改成
sTail.next = lHead.next;
public class Solution { //CC150 public ListNode partition(ListNode head, int x) { if(head == null) return null; ListNode sHead = new ListNode(-1);//small ListNode sTail = sHead; ListNode lHead = new ListNode(-1);//large ListNode lTail = lHead; while(head != null){ if(head.val < x){ sTail.next = head; sTail = sTail.next; } else{ lTail.next = head; lTail = lTail.next; } head = head.next; } sTail.next = lHead.next; lTail.next = null; return sHead.next; }}
0 0
- 2.2.3 Partition List
- 2.2.3 Partition List
- 2.2.3—单链表— Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- Partition List
- 内存Bank
- PAT 1040. Longest Symmetric String (25)
- 教你使用Navicat Lite新建mysql用户、数据库 by DedeCMS8
- C学习笔记2-int类型数据在内存中的存储形式
- I'm coming!
- 2.2.3 Partition List
- 算法面试题
- Leetcode Interleaving String
- X窗口系统的协议和架构
- 呼呼呼呼呵呵呵呵呵
- 你你你你你您 我我我我我
- 2.2.4 Remove Duplicates from Sorted List
- 使用WM_MOUSEWHEEL
- hdu1525 Euclid's Game , 基础博弈