[英]Merging two linked lists alternately while looking for a maximum as the head
我正在開發一個鏈表程序,該程序只能循環遍歷該列表一次,而且無法將列表的元素復制到另一個數據結構中。
假設列表不為空(至少有一個節點),而最后一個節點的下一個為空。
下面的方法應該合並兩個鏈表,合並表的頭應該是兩個鏈表中的最大值。 以下值在兩個列表之間交替顯示。
例如,如果我的輸入是:
1 2 4
3 5 6 7 8 9
我的輸出將是:
3 1 5 2 6 4 7 8 9
我想出了如何分配頭的方法,但是我想不出如何正確地合並他的其余部分。
這是我的代碼:
public class LinkedListNode {
private int value;
private LinkedListNode next;
public LinkedListNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
}
public static LinkedListNode mergeLists (LinkedListNode head1, LinkedListNode head2){
LinkedListNode firstList = head1;
LinkedListNode secondList = head2;
LinkedListNode tmp = new LinkedListNode(0);
LinkedListNode result = head1;
int checkWhichList=0;
while(firstList!=null && secondList!=null){
if (firstList.getValue()<=secondList.getValue()){
result=secondList;
}
if (checkWhichList%2==0){
tmp.setNext(head2.getNext());
checkWhichList++;
}
else
tmp.setNext(head1.getNext());
}
result.setNext(tmp);
return result;
}
一旦確定哪個節點必須添加兩個頭最大值result
(第一最大值),然后將您的指針firstList
和secondList
現在,您繼續從兩個列表中添加結果,同時移動到每個列表中的下一個節點,直到兩個都沒有指向任何對象為止
示例:(以firstList為頭的情況為例)
if(firstList.getValue() > secondList.getValue()) {
//here firstList will be the head
result = firstList;
result.setNext(secondList);
firstList = firstList.getNext();
secondList = secondList.getNext();
//Now alternating (adding firstList node before secondList)
while(firstList != null || secondList != null) {
if(firstList != null) {
result.setNext(firstList);
firstList = firstList.getNext();
}
if(secondList != null) {
result.setNext(secondList);
secondList = secondList.getNext();
}
}
} else {
//here secondList will be the head
//continue with the code
}
您可以嘗試以下代碼:
public class Test {
public static void main(String args[]) {
//1 2 4
//3 5 6 7 8 9
LinkedListNode list1 = new LinkedListNode(1);
list1.insert(list1, 2);
list1.insert(list1, 4);
LinkedListNode list2 = new LinkedListNode(3);
list2.insert(list2, 5);
list2.insert(list2, 6);
list2.insert(list2, 7);
list2.insert(list2, 8);
list2.insert(list2, 9);
LinkedListNode result = mergeLists(list1, list2);
while(result!=null){
System.out.print(result.getValue() + " ");
result = result.getNext();
}
}
public static LinkedListNode mergeLists (LinkedListNode head1, LinkedListNode head2){
LinkedListNode firstList = head1;
LinkedListNode secondList = head2;
LinkedListNode tmp = new LinkedListNode(0);
LinkedListNode result = new LinkedListNode(0);
int checkWhichList=0;
if(firstList!=null && secondList!=null){
if (firstList.getValue()<=secondList.getValue()){
result.insert(result, secondList.getValue());
secondList = secondList.getNext();
checkWhichList++;
}else{
result.insert(result, firstList.getValue());
firstList = firstList.getNext();
//checkWhichList++;
}
}
while(firstList!=null && secondList!=null) {
if (checkWhichList % 2 != 0) {
result.insert(result, firstList.getValue());
firstList = firstList.getNext();
checkWhichList++;
} else{
result.insert(result, secondList.getValue());
secondList = secondList.getNext();
checkWhichList++;
}
}
if(firstList!=null){
while(firstList!=null) {
result.insert(result, firstList.getValue());
firstList = firstList.getNext();
}
}
if(secondList!=null){
while(secondList!=null) {
result.insert(result, secondList.getValue());
secondList = secondList.getNext();
}
}
//result.setNext(tmp);
return result.getNext();
}
}
class LinkedListNode {
private int value;
private LinkedListNode next;
public LinkedListNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
public LinkedListNode insert(LinkedListNode head, int value){
LinkedListNode temp = new LinkedListNode(value);
LinkedListNode temp1 = head;
if(head == null){
return temp;
}
else{
temp1 = head;
while(temp1.next != null){
temp1 = temp1.next;
}
temp1.next = temp;
}
return head;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.