新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
關(guān)于鏈表的拓展基本操作
一.通過X將鏈表排序;小的在前大的在后:
思考:遍歷整個鏈表,與X作比較,小的尾插在SMALL里,大的尾插在BIG 里;遍歷結(jié)束,判斷SMALL或者BIG是否為空,如果是則只返回另一個的第一個結(jié)點(diǎn),
十年的鹽邊網(wǎng)站建設(shè)經(jīng)驗(yàn),針對設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整鹽邊建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)建站從事“鹽邊網(wǎng)站設(shè)計(jì)”,“鹽邊網(wǎng)站推廣”以來,每個客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
class ListNode {
public int val;
public ListNode next;
public ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
public ListNode(int val) {
this(val, null);
}
}
public class LinkedListInterview {
public ListNode separateByX(ListNode head, int x) {
ListNode sHead = null;
ListNode sEnd = null;
ListNode bHead = null;
ListNode bEnd = null;
for (ListNode cur = head; cur != null; cur = cur.next) {
if (cur.val < x) {
if (sHead == null) {
sHead = cur;
} else {
sEnd.next = cur;
}
sEnd = cur;
} else {//≥
if (bHead == null) {
bHead = cur;
} else {
bEnd.next = cur;
}
bEnd = cur;
}
}
if (sEnd == null) {
return bHead;
}
sEnd.next = bHead;
if (bEnd != null) {
bEnd.next = null;
}
return sHead;
}
private static ListNode createTestList() {
ListNode n1 = new ListNode(4);
ListNode n2 = new ListNode(5);
ListNode n3 = new ListNode(2);
ListNode n4 = new ListNode(7);
ListNode n5 = new ListNode(6);
ListNode n6 = new ListNode(3);
ListNode n7 = new ListNode(8);
ListNode n8 = new ListNode(1);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = n5;
n5.next = n6;
n6.next = n7;
n7.next = n8;
return n1;
}
// 三個重要節(jié)點(diǎn)
// 1. 咱創(chuàng)建的測試鏈表是不是出問題了?
// 2. 咱的分離程序是不是出問題了?
// 3. 咱的打印程序是不是出問題了?
private static void test() {
// 4 5 2 7 6 3 8 1
ListNode head = createTestList();
ListNode result = new LinkedListInterview().separateByX(head, 5);
// 4 2 3 1 5 7 6 8
for (ListNode cur = result; cur != null; cur = cur.next) {
System.out.println(cur.val);
}
}
public static void main(String[] args) {//主函數(shù),調(diào)用test子函數(shù)
test();
}
}
分享名稱:關(guān)于鏈表的拓展基本操作
網(wǎng)站網(wǎng)址:http://fisionsoft.com.cn/article/jihshp.html