說明:思路中寫的是代碼,表達(dá)基本意思
一、刪除鏈表中所有與val相等的元素
定義兩個(gè)結(jié)點(diǎn):prev和cur
遍歷整個(gè)鏈表:
相等:prve.next=cur.next
cur=cur.next
prev=prev.next
不相等:cur=cur.next
二、合并兩個(gè)有序鏈表
定義兩個(gè)結(jié)點(diǎn)result(合成的新鏈表的頭結(jié)點(diǎn)) last(result的最后一個(gè)結(jié)點(diǎn))
如果cur1.val<=cur2.val
last.next=cur1
cur1=cur1.next
否則,同理,更新cur2
```class Node {
int val;
Node next = null;
public Node(int val) {
this.val = val;
}
}
public class Solution {
Node removeAll(Node head, int val) {
Node prev = null;
Node cur = head;
while (cur != null) {
if (cur.val == val) {
if (cur == head) {
head = cur.next;
} else {
prev.next = cur.next;
}
} else {
prev = cur;
}
cur = cur.next;
}
return head;
}
Node merge(Node head1, Node head2) {
if (head1 == null) {
return head2;
}
if (head2 == null) {
return head1;
}
Node result = null;
Node last = null;
Node cur1 = head1;
Node cur2 = head2;
while (cur1 != null && cur2 != null) {
if (cur1.val <= cur2.val) {
if (result == null) {
result = cur1;
} else {
last.next = cur1;
}
last = cur1;
cur1 = cur1.next;
} else {
if (result == null) {
result = cur2;
} else {
last.next = cur2;
}
last = cur2;
cur2 = cur2.next;
}
}
if (cur1 != null) {
last.next = cur1;
} else {
last.next = cur2;
}
return result;
}
public static Node createList() {
Node n1 = new Node(6);
Node n3 = new Node(2);
Node n4 = new Node(6);
Node n6 = new Node(4);
Node n8 = new Node(6);
n1.next = n3;
n3.next = n4;
n4.next = n6;
n6.next = n8;
return n1;
}
public static Node createList1() {
Node n1 = new Node(1);
Node n2 = new Node(2);
n1.next = n2;
return n1;
}
public static Node createList2() {
Node n1 = new Node(1);
Node n2 = new Node(3);
Node n3 = new Node(5);
Node n4 = new Node(7);
n1.next = n2;
n2.next = n3;
n3.next = n4;
return n1;
}
public static void main(String[] args) {
Node head = createList();
Node result = new Solution().removeAll(head, 6);
for (Node cur = result; cur != null; cur = cur.next) {
System.out.println(cur.val);
}
System.out.println("=====================");
Node head1 = createList1();
Node head2 = createList2();
Node merged = new Solution().merge(head1, head2);//類中的函數(shù)返回值是Node類型,用merge接收
for (Node cur = merged; cur != null; cur = cur.next) {//merge相當(dāng)于head,代表整個(gè)鏈表
System.out.println(cur.val);
}
}
}
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
分享標(biāo)題:關(guān)于鏈表:removeAll()和mergeTwoList()-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://jinyejixie.com/article20/picjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、App設(shè)計(jì)、面包屑導(dǎo)航、品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站制作、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容