日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

[LeetCode] 86. Partition List Java

發布時間:2024/10/12 java 76 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [LeetCode] 86. Partition List Java 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目:

Given a linked list and a value?x, partition it such that all nodes less than?x?come before nodes greater than or equal to?x.

You should preserve the original relative order of the nodes in each of the two partitions.

For example,
Given?1->4->3->2->5->2?and?x?= 3,
return?1->2->2->4->3->5.

題意及分析:給出一個鏈表和一個值x,將鏈表中值小于x的點移動到值大于等于x的點之前,分別需要保持保持兩部分中點的相對位置不變。可以分別得到大于等于x的鏈表和小于x的鏈表,然后把兩個鏈表組合起來就是需要的結果。

代碼:

/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/ public class Solution {public ListNode partition(ListNode head, int x) {ListNode list1=new ListNode(0);ListNode list2=new ListNode(0);ListNode p1=list1;ListNode p2=list2;ListNode start=head;while(start!=null){int value=start.val;if(value<x){p1.next = start;p1=p1.next;// list1.next=null;}else{p2.next = start;p2=p2.next;}start=start.next;}p2.next=null;p1.next=list2.next;return list1.next;} }

?

轉載于:https://www.cnblogs.com/271934Liao/p/8045988.html

總結

以上是生活随笔為你收集整理的[LeetCode] 86. Partition List Java的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。