SORU
8 NİSAN 2010, PERŞEMBE


Nasıl tek bağlı bir liste sonundan N. eleman bulmak için?

Aşağıdaki işlevi için nth bulmaya çalışıyorsonbir tek öğe listesi bağlı.

Örneğin:

Eğer öğeleri 8->10->5->7->2->1->5->4->10->10 Daha sonra ise sonuç. 7th son düğüme 7.

Herkes bu kodu yoksa daha iyi ve daha basit bir yaklaşım olduğunu nasıl çalıştığını bana yardım edebilir mi?

LinkedListNode nthToLast(LinkedListNode head, int n) {
  if (head == null || n < 1) {
  return null;
}
  LinkedListNode p1 = head;
  LinkedListNode p2 = head;
  for (int j = 0; j < n - 1;   j) { // skip n-1 steps ahead
  if (p2 == null) {
       return null; // not found since list size < n
   }
  p2 = p2.next;
  }
  while (p2.next != null) {
  p1 = p1.next;
  p2 = p2.next;
 }
   return p1;
 }

CEVAP
9 NİSAN 2010, Cuma


Anahtar bu algoritma için ayarlanmış iki işaretçiler p1 p2 ayrı n-1 düğüm başlangıçta çok istiyoruz p2 noktasına (n-1)th düğümden başlangıç listesinden sonra biz hareket p2 kadar ulaşır last düğüm listesi. p2 listenin sonuna geldiğinde p1 listenin sonuna N. düğüme işaret eder.

Yorum olarak açıklama satır içi koydum. Umarım yardımcı olur:

// Function to return the nth node from the end of a linked list.
// Takes the head pointer to the list and n as input
// Returns the nth node from the end if one exists else returns NULL.
LinkedListNode nthToLast(LinkedListNode head, int n) {
  // If list does not exist or if there are no elements in the list,return NULL
  if (head == null || n < 1) {
    return null;
  }

  // make pointers p1 and p2 point to the start of the list.
  LinkedListNode p1 = head;
  LinkedListNode p2 = head;

  // The key to this algorithm is to set p1 and p2 apart by n-1 nodes initially
  // so we want p2 to point to the (n-1)th node from the start of the list
  // then we move p2 till it reaches the last node of the list. 
  // Once p2 reaches end of the list p1 will be pointing to the nth node 
  // from the end of the list.

  // loop to move p2.
  for (int j = 0; j < n - 1;   j) { 
   // while moving p2 check if it becomes NULL, that is if it reaches the end
   // of the list. That would mean the list has less than n nodes, so its not 
   // possible to find nth from last, so return NULL.
   if (p2 == null) {
       return null; 
   }
   // move p2 forward.
   p2 = p2.next;
  }

  // at this point p2 is (n-1) nodes ahead of p1. Now keep moving both forward
  // till p2 reaches the last node in the list.
  while (p2.next != null) {
    p1 = p1.next;
    p2 = p2.next;
  }

   // at this point p2 has reached the last node in the list and p1 will be
   // pointing to the nth node from the last..so return it.
   return p1;
 }

Alternatif olarak ayarlayabilirsiniz p1 p2 apart tarafından n düğüm yerine (n-1) ve hareket p2 sonuna kadar kontrol listesi yerine hareket kadar son düğüm:

LinkedListNode p1 = head;
LinkedListNode p2 = head;
for (int j = 0; j < n ;   j) { // make then n nodes apart.
    if (p2 == null) {
        return null;
    }
    p2 = p2.next;
}
while (p2 != null) { // move till p2 goes past the end of the list.
    p1 = p1.next;
    p2 = p2.next;
}
return p1;

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • filmurfreakur

    filmurfreaku

    29 Mart 2007
  • SellerDp

    SellerDp

    27 EKİM 2009
  • TheFlashCentre

    TheFlashCent

    22 Mayıs 2008