2017-12-30 8 views
-1

를 연결이 나는이 내 코드입니다어떻게 자바에서 마지막 노드에 첫 번째 노드를 대체 할 목록

첫 번째 노드,하지만 거기에 뭔가 문제가 지난 노드와 마지막 노드에 첫 번째 노드를 대체 싶어 안녕 : 어떤 도움?을 간단한 코드 아래

public void replaceNode() { 

    Node firstNode = head.next; 
    Node lastNode = null; 

    location = head; 
    while (head != null) { 
     location = location.next; 
     predLocation = location; 
     lastNode = location; 
    } 

    head.next = lastNode; 
    lastNode.next = head.next.next; 

    firstNode.next = null; 
    predLocation.next = firstNode; 

} 
+1

참조하십시오 : [왜 "누군가가 나를 도와 드릴까요?"입니다하지 실제 질문을?] (http://meta.stackoverflow.com/q/284236) [디버깅 도움을 구하고 질문 (* "왜 ISN을 이 코드는 작동하지 않습니까? ")는 원하는 동작, 특정 문제 또는 오류 및 질문 자체에서이를 재현하는 데 필요한 가장 짧은 코드를 포함해야합니다. 명확한 문제 설명이없는 질문은 다른 독자에게 유용하지 않습니다.] (https://stackoverflow.com/help/on-topic) 참고 : [최소의 완전하고 검증 가능한 예제를 만드는 방법] (https : // stackoverflow .com/help/mcve). – Turing85

답변

0

봅니다 ..

import java.util.Scanner; 

public class swapHeadTail{ 

static llistNode insert(llistNode H,int data){ 
    llistNode tmp = new llistNode(); 
    tmp.data=data; 
    tmp.next=null; 
    if(H==null) 
     return tmp; 
    llistNode curr = new llistNode(); 
    curr = H; 
    while(curr.next != null) 
     curr = curr.next; 
    curr.next = tmp; 
    return H; 
} 

public static void main(String[] args) { 
    llistNode H = new llistNode(); 
    Scanner in = new Scanner(System.in); 
    llistNode tmp =new llistNode(); 

    H=null; 
    do{ 
     System.out.print("Enter data:"); 
     H=insert(H,in.nextInt()); 
     System.out.print("Enter 1 to continue: "); 
    }while (in.nextInt()==1); 

    tmp = H; 
    while(tmp != null){ 
     System.out.print(tmp.data+"\t"); 
     tmp = tmp.next; 
    } 
    System.out.println(); 

    llistNode head = new llistNode(); 
    llistNode headNext = new llistNode(); 
    llistNode tailPrev = new llistNode(); 
    llistNode tail = new llistNode(); 

    head = H; 
    headNext = H.next; 
    tailPrev = H; 
    while(tailPrev.next.next != null) 
     tailPrev=tailPrev.next; 
    tail = tailPrev.next; 
    if ((tailPrev == head)&&(headNext == tail)){ 
     head.next = null; 
     tail.next = head; 
    } 
    else{ 
     tmp = head; 
     tmp.next = null; 
     tailPrev.next = tmp; 
     tail.next=headNext; 
    } 
    H=tail; 
    tmp = H; 
    while(tmp != null){ 
     System.out.print(tmp.data+"\t"); 
     tmp = tmp.next; 
    } 

    } 
} 

가 심판 : 그것은 불변 link.If http://rawjava.blogspot.in/2015/04/java-program-to-swap-head-and-tail-of.html

+0

코드 전용 답변은 권장하지 않습니다. 설명을 추가해야합니다. 외부 링크의 경우 향후 URL을 사용할 수없는 경우 콘텐츠 요약을 제공해야합니다. – Turing85

0

은 단순히 당신이 변경하지 않고 두 노드 사이의 값을 변경할 수는 다음을 할 수 있습니다 아래 코드를 사용하십시오.

public void replaceNode(ListNode head) { 
      ListNode firstNode = head.next; 
      ListNode tempNode = head; 
      ListNode preLastNode = null; 
      while(tempNode.next !=null) { 
       preLastNode=tempNode; 
       tempNode=tempNode.next; 
      } 
      head.next=preLastNode.next; 
      head.next.next=firstNode.next; 
      preLastNode.next=firstNode; 
      preLastNode.next.next=null; 
     } 
관련 문제