Skip to content

Instantly share code, notes, and snippets.

@Kelvinson
Created July 12, 2017 06:51
Show Gist options
  • Save Kelvinson/9b0729afffb8bcd837c30db9548b3168 to your computer and use it in GitHub Desktop.
Save Kelvinson/9b0729afffb8bcd837c30db9548b3168 to your computer and use it in GitHub Desktop.
LinkList reverse and normal order construction
public static Node reverseAndClone(Node list) {
Node head = null;
while (list != null) {
Node n = new Node(list.val);
n.next = head;
head = n;
list = list.next;
}
return head;
}
// the difference between normal order and reverse order is head.next = n;
public static Node cloneList(Node list) {
Node head = null;
while (list != null) {
Node n = new Node(list.val);
head.next = n; // this is the difference between cloneAndReverse(Node list)
head = n;
list = list.next;
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment