[英]Swapping nodes in a singly linked list java
我一直在嘗試想出一種算法來交換單向鏈表中的 2 個節點(不一定彼此相鄰)2 天,但由於某種原因我不能這樣做。 這就是我所擁有的,我對編碼真的很陌生並且壓力很大: 我設法放置了一個臨時節點,但實際上無法交換節點。
public void swap(int i, int j) {
current = head;
current2 = head;
sllNode temp = new sllNode(" ");
sllNode temp2 = new sllNode(" ");
for(int z = 0; i>z; z++)
current=current.next;
for(int q = 0; j>q; q++)
current2 = current2.next;
temp.next = current2.next.next;
current.next = temp;
current.next = current2.next.next;
current2.next = current;
為什么可以交換節點,何時可以交換數據?
public void swap(int i, int j) {
sllNode ithNode = head;
for (int z = 0; z < i; z++) {
ithNode = ithNode.next;
}
sllNode jthNode = head;
for (int q = 0; q < j; q++) {
jthNode = jthNode.next;
}
// Swap the data
String data = ithNode.data;
ithNode.data = jthNode.data;
jthNode.data = data;
}
使用一種方法很有意義:
public sllNode get(int i) {
sllNode current = head;
while (i > 0) {
current = current.next;
}
return current;
}
順便說說:
SllNode
。 current
和current2
可以作為局部變量的字段。 交換節點,艱難的方式
這里必須要思考,因此最好先處理特殊情況,然后再處理i < j
。
public void swap(int i, int j) {
if (i >= size() || j >= size()) {
throw new IndexOutOfBoundsException();
}
if (i == j) {
return;
}
if (j < i) {
swap(j, i);
return;
}
// i < j
sllNode ithPredecessor = null;
sllNode ithNode = head;
for (int z = 0; z < i; z++) {
ithPredecessor = ithNode;
ithNode = ithNode.next;
}
sllNode jthPredecessor = ithNode;
sllNode jthNode = ithNode.next;
for (int q = i + 1; q < j; q++) {
jthPredecessor = jthNode;
jthNode = jthNode.next;
}
// Relink both nodes in the list:
// - The jthNode:
if (ithPredecessor == null) {
head = jthNode;
} else {
ithPredecessor.next = jthNode;
}
sllNode jNext = jthNode.next;
//if (ithNode.next == jthNode) {
if (jthPredecessor == ithNode) {
jthNode.next = ithNode;
} else {
jthNode.next = ithNode.next;
}
// - The ithNode:
if (jthPredecessor == ithNode) {
} else {
jthPredecessor.next = ithNode;
}
ithNode.next = jNext;
}
無法保證邏輯是正確的。 有技巧:
//if (ithNode.next == jthNode) {
if (jthPredecessor == ithNode) {
這兩個條件都測試i + 1 == j
,但是在.next上進行測試然后賦值使該條件成為瞬時狀態。 如您所見,如果有一個if (i + 1 == j) { ... } else { ... }
並同時處理ithNode和jthNode,那將更容易。
為此,您需要交換兩件事:上一個節點的下一個節點和下一個節點。
找到要交換的節點的先前節點current
和current2
,請執行以下操作:
交換節點:
sllNode tmp = current.next;
current.next = current2.next;
current2.next = tmp;
然后交換下一個:
tmp = current.next.next;
current.next.next = current2.next.next;
current2.next.next = tmp;
// 使用 Java 交換鏈表中的兩個元素
導入 java.util.*;
class SwappingTwoElements {
public static void main(String[] args)
{
LinkedList<Integer> ll = new LinkedList<>();
// Adding elements to Linked List
ll.add(10);
ll.add(11);
ll.add(12);
ll.add(13);
ll.add(14);
ll.add(15);
// Elements to swap
int element1 = 11;
int element2 = 14;
System.out.println("Linked List Before Swapping :-");
for (int i : ll) {
System.out.print(i + " ");
}
// Swapping the elements
swap(ll, element1, element2);
System.out.println();
System.out.println();
System.out.println("Linked List After Swapping :-");
for (int i : ll) {
System.out.print(i + " ");
}
}
// Swap Function
public static void swap(LinkedList<Integer> list,
int ele1, int ele2)
{
// Getting the positions of the elements
int index1 = list.indexOf(ele1);
int index2 = list.indexOf(ele2);
// Returning if the element is not present in the
// LinkedList
if (index1 == -1 || index2 == -1) {
return;
}
// Swapping the elements
list.set(index1, ele2);
list.set(index2, ele1);
}
} Output 在交換鏈表之前:- 10 11 12 13 14 15
交換鏈表后:- 10 14 12 13 11 15 時間復雜度:O(N),其中 N 是鏈表長度
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.