鱼C论坛

 找回密码
 立即注册
查看: 1635|回复: 0

[学习笔记] Leetcode 86. Partition List

[复制链接]
发表于 2019-8-5 02:28:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
3.jpg



  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. */
  9. class Solution {
  10.     public ListNode partition(ListNode head, int x) {
  11.         
  12.         if(head == null){
  13.             
  14.             return null;
  15.         }
  16.         
  17.         ListNode less_head = new ListNode(0);
  18.         ListNode more_head = new ListNode(0);
  19.         ListNode less_ptr = less_head;
  20.         ListNode more_ptr = more_head;
  21.         
  22.         while(head != null){
  23.             
  24.             if(head.val >= x){
  25.                
  26.                 more_ptr.next = head;
  27.                
  28.                 more_ptr = more_ptr.next;
  29.                
  30.             }
  31.             else{
  32.                
  33.                
  34.                 less_ptr.next = head;
  35.                
  36.                 less_ptr = less_ptr.next;
  37.                
  38.             }
  39.             
  40.             head = head.next;
  41.             
  42.         }
  43.         
  44.         less_ptr.next = more_head.next;
  45.         more_ptr.next = null;
  46.         
  47.         return less_head.next;
  48.         
  49.     }
  50. }
复制代码

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 20:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表